Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Odd numbers

 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm i dont know how to get an odd number,
lets say i create a loop,
for(int i; i<=num; i++)
the number will go from one to whatever number the user entered, now, how do i filter the odd numbers??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way: Start with an even or odd (0 or 1), depending on what you want. Then instead of incrementing by one using i++, increment by two using i+=2.

Here's another way: Increment i as before. Then int odd = (2*i) + 1.
[ April 11, 2006: Message edited by: marc weber ]
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is what i got:
Enter a number:
28


The perfect numbers are: 6
The perfect numbers are: 28

The Abundant numbers are: 14
The Abundant numbers are: 20
The Abundant numbers are: 26
though i need the odd numbers
thanks for the support

EDIT: actually thats an error, 14 is not an abundant number, the factors of 14(not counting the number itself) are 2+7=9<i, and it needs to be >i
[ April 11, 2006: Message edited by: apollo abel ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to test for odd numbers:

if (num % 2 != 0)
num is odd
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well this is what i ended up with:
What is your name?
neo

Enter a number:
5000


6: Perfect number
28: Perfect number
496: Perfect number

945: Odd abundant number
1575: Odd abundant number
2205: Odd abundant number
2835: Odd abundant number
3465: Odd abundant number
4095: Odd abundant number
4725: Odd abundant number
do you guys thing the answer is right??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by apollo abel:
...do you guys thing the answer is right??

Well, it found the 3 perfect numbers in that range (the 4th isn't until 8128). And it found the first abundant odd number, 945, as well as odd multiples of 945 within range, 2835 and 4725. So the results look plausible.

The real questions are:
  • How confident are you in your algorithm?
  • Do you have a way to verify test results?
  • (If this page is accurate, there are 2487 abundant numbers below 10,000. You might try using your code to count these, and see if you get the same result.)
    [ April 11, 2006: Message edited by: marc weber ]
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by apollo abel:
    ...
    do you guys thing the answer is right??


    Well, that depends. What is the question?

    Layne
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic