d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Help
12Next
Add Reply New Topic New Poll
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 29 2014 08:55pm
Hey, so I basically have to find all of the Armstrong number from 1 to 1000.

Here's my code so far.

Code
for (int n=1; n <1000; n++){

int sum = 0;

int number =n;

int original = number;

while(number>0){

int t= number%10;

sum += t*t*t;

number = number/10;

}

if (sum == original) {

System.out.println(original);

}

}

}

}

It works, well kind of, I get 1, 153, 370, 371 and 407.
The thing is that I don't get why im not getting the numbers 2-3-4-5-6-7-8-9.
It's probably just a small fix but I don't know.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 09:27pm
I don't understand your issue. The Armstrong numbers in the range 1-1000 are 1, 153, 370, 371 and 407. Which is what you are getting. What exactly is the problem?
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 29 2014 09:41pm
Quote (Minkomonster @ Oct 29 2014 11:27pm)
I don't understand your issue. The Armstrong numbers in the range 1-1000 are 1, 153, 370, 371 and 407. Which is what you are getting. What exactly is the problem?


The Armstrong numbers in the range of 1-1000 are 1-2-3-4-5-6-7-8-9-153-370-371-407.

You can check on the wiki.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 09:46pm
Quote (Jibbosh @ Oct 29 2014 10:41pm)
The Armstrong numbers in the range of 1-1000 are 1-2-3-4-5-6-7-8-9-153-370-371-407.

You can check on the wiki.


Prove it.

I'll get you started

2*2*2 != 2
3*3*3 != 3
4*4*4 != 4


Continue. Show me how 2,3,4,5,6,7,8,9 are Armstrong numbers.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 29 2014 09:49pm
/edit: nevermind, didn't see minko's post

This post was edited by carteblanche on Oct 29 2014 09:49pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 09:51pm
If you are looking for a general Armstrong number that is a number whose digits are raised to the power of the number of digits, then 2,3,4,5,6,7,8,9 would be included, but if you are looking for Armstrong numbers of 3 digits, as you are representing by your code, then they would not be included.
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 29 2014 09:57pm
Quote (Minkomonster @ Oct 29 2014 11:46pm)
Prove it.

I'll get you started

2*2*2 != 2
3*3*3 != 3
4*4*4 != 4


Continue. Show me how 2,3,4,5,6,7,8,9 are Armstrong numbers.


Jesus, I ask for help and I get an arrogant dude answering me with something that doesn't help at all. If im wrong, just say it politely.

Quote (Minkomonster @ Oct 29 2014 11:51pm)
If you are looking for a general Armstrong number that is a number whose digits are raised to the power of the number of digits, then 2,3,4,5,6,7,8,9 would be included, but if you are looking for Armstrong numbers of 3 digits, as you are representing by your code, then they would not be included.


Well , I guess you understood.
Alright , so could I just do a IF the number has 2 digits, or 1 in that case and do like : sum + = t*t for 2 number digits and just sum+=t and that would work? Imma try it out.

This post was edited by Jibbosh on Oct 29 2014 09:58pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 10:03pm
Quote (Jibbosh @ Oct 29 2014 10:57pm)
Jesus, I ask for help and I get an arrogant dude answering me with something that doesn't help at all.



Well , I guess you understood.
Alright , so could I just do a IF the number has 2 digits, or 1 in that case and do  like : sum + = t*t for 2  number digits and just sum+=t and that would work? Imma try it out.


I am not being arrogant, and I am not being patronizing. I am using your code to point out your flaws. If I was arrogant, I wouldn't bother helping you at all. The point of my questioning was to get you to think about what your code is doing. Your code is assuming each number is 3 digits, which if you treated 0 as 000, 1 as 001, 2 as 002, etc then your code would be correct. But that is clearly not what you want. My other posts were intended to get you to think about the choices you made when summing up your digits and stumble upon the fact that you are treating each number as a 3 digit, when that is not the case.

Now, back to the question at hand, no. Don't do that. You dont want to have 3 if statements to determine how many digits the number has. Because if you have the data to calculate how many digits the number has then the if statements are not needed. Instead, calculate how many digits your number has, do your logic in your while loop to parse each individual digit, and once you have each digit raise it to the power of the number of digits you calculated and add it to your sum.


Edit: Because you felt insulted by me, I will throw you a bone. Here is a trick to find the number of digits in an integer using Strings

Code
int num = 100;
int numOfDigits = String.valueOf(num).length(); // numOfDigits is 3 because the length of the String is 3


This post was edited by Minkomonster on Oct 29 2014 10:08pm
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Oct 29 2014 10:17pm
Quote (Minkomonster @ Oct 30 2014 12:03am)
I am not being arrogant, and I am not being patronizing. I am using your code to point out your flaws. If I was arrogant, I wouldn't bother helping you at all. The point of my questioning was to get you to think about what your code is doing. Your code is assuming each number is 3 digits, which if you treated 0 as 000, 1 as 001, 2 as 002, etc then your code would be correct. But that is clearly not what you want. My other posts were intended to get you to think about the choices you made when summing up your digits and stumble upon the fact that you are treating each number as a 3 digit, when that is not the case.

Now, back to the question at hand, no. Don't do that. You dont want to have 3 if statements to determine how many digits the number has. Because if you have the data to calculate how many digits the number has then the if statements are not needed. Instead, calculate how many digits your number has, do your logic in your while loop to parse each individual digit, and once you have each digit raise it to the power of the number of digits you calculated and add it to your sum.


Edit: Because you felt insulted by me, I will throw you a bone. Here is a trick to find the number of digits in an integer using Strings

Code
int num = 100;
int numOfDigits = String.valueOf(num).length(); // numOfDigits is 3 because the length of the String is 3


Didn't feel insulted, it was just a bit rude imo, you probably didn't mean it but that's how I felt it, thanks anyways, I understand now.

Nvm, just saw the int string value conversion in the second line, im tired, lol.

This post was edited by Jibbosh on Oct 29 2014 10:21pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 10:23pm
Quote (Jibbosh @ Oct 29 2014 11:17pm)
Didn't feel insulted, it was just a bit rude imo, you probably didn't mean it but that's how I felt it, thanks anyways, I understand now.

And wouldn't your code be ..

String num = 100 ; ?? since you said //numofdigits is 3 because the length of the String is 3, but you put an integer.


No, its an integer. The String I was refering to is the the return of String.valueOf(num), which would return "100". Calling the length method on this string returns an integer representing the length of the string.

To be more clear

Code
int num = 100;
String stringifiedNum = String.valueOf(num);
int numOfDigits = stringifiedNum.length(); //numOfDigits is 3 because the length of the String is 3
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll