As an assignment I need to count the amount of happy numbers* in a given range. I did most of that already:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String inputLowerRange = scan.nextLine().trim();
String inputUpperRange = scan.nextLine().trim();
int lowerRange = Integer.parseInt(inputLowerRange);
int upperRange = Integer.parseInt(inputUpperRange);
Set<Integer> numbers = new HashSet<Integer>();
for (int i = lowerRange; i <= upperRange; i )
while(lowerRange>0) {
upperRange = (lowerRange % 10)*(lowerRange % 10);
lowerRange /=10;
}
int counter = 0;
for (int i = lowerRange; i <= upperRange; i ) {
}
System.out.println(counter);
}
}
Now the code just needs to count the happy numbers* present within that range. I understand it is best done with:
if ............... counter
but how exactly would I need to do it?
*A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. 13 is a happy number because 1^2 3^2 = 10 And 1^2 0^2 = 1, thus 13 is a happy number.
CodePudding user response:
This might work:
public boolean isHappy(Set<Integer> previousNumbers, int number) {
if (previousNumbers == null) previousNumbers = new HashSet<>();
else if (previousNumbers.contains(number)) return false;
if (number == 4) return true;
int length = getNumberLength(number);
int subNumber = 0;
for (int i = 0; i < length; i ) {
int digit = getNumberDigit(number, i);
subNumber = digit * digit;
}
previousNumbers.add(number);
return isHappy(subNumber);
}
public int getNumberDigit(int number, int index) {
return number % (int) Math.pow(10, index 1);
}
public int getNumberLength(int number) {
int length = 0;
for (long temp = 1; temp <= number; temp *= 10)
length ;
return length;
}
You could call it like this:
if (isHappy(null, number))
CodePudding user response:
int counter = 0;
for(int i=lowerRange;i<=upperRange;i )
{
int num=i,sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum =(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1)
counter
}
System.out.println(counter);
the issue with your code is you are making direct changes to lowerRange
and upperRange
which will result in not iterating properly. You need to use temp variables for those. Also you forgot this condition while(sum!=1 && sum!=4){}
Set<Integer> numbers = new HashSet<Integer>(); //what's this for?
for (int i = lowerRange; i <= upperRange; i )
while(lowerRange>0) { // missed one outer while loop
upperRange = (lowerRange % 10)*(lowerRange % 10); // changes to upperRange
lowerRange /=10; // changes to lowerRange
}
CodePudding user response:
Try this.
static boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while (n > 1 && !set.contains(n)) {
set.add(n);
n = String.valueOf(n).chars()
.map(c -> c - '0')
.reduce(0, (sum, i) -> sum i * i);
}
return n == 1;
}
public static void main(String args[]) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println(IntStream.rangeClosed(scan.nextInt(), scan.nextInt())
.filter(i -> isHappy(i))
.count());
}