2012-10-01T08:15:35-04:00
Happy Numbers.. Java Program
A happy number is built by adding the squares of its digits, doing this permanently, the numbers will end in 1 or 4.
if a positive integer ends with 1, then it is a happy number.
the first few happy numbers are: 1,7,10,13,19,23,28 ...
solution : 13 = 1^2 +3 ^2 = 1+9 = 10
10 = 1^2 + 0+2 = 1+0 = 1
how many positive happy numbers are there in less than 300,000?
1
2012-10-02T07:35:39-0400
public class main { /** * @param args */ public static void main(String[] args) { int count=0; for(int i=1;i<300000;i++) { int var=i; var=getSqSumm(var); while(var>9) { var=getSqSumm(var); } if(var==4||var==1) { count++; } } System.out.println(count); } public static int getSqSumm(int numb) { int var=numb; int res=0; while(var>0) { res+=Math.pow(var%10,2); var/=10; } return res; } }
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS !
Learn more about our help with Assignments:
Java JSP JSF
Comments