perfect numbers . . !

Reply

Join Date: Nov 2009
Posts: 24
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster

perfect numbers . . !

 
0
  #1
Nov 20th, 2009
Can any one help me with this exercise . . !
I did not understnd it . ?





A number is called a proper divisor of N if M < N and M divides N. A positive integer is called perfect if it is the sum of its positive proper divisors. For example, the positive proper divisors of 28 are 1, 2, 4, 7, and 14 and 1+2+4+7+14=28. Therefore, 28 is perfect. Write a program to display the first 4 perfect integer ..
Last edited by alreem; Nov 20th, 2009 at 5:26 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 59
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training
 
0
  #2
Nov 20th, 2009
Originally Posted by alreem View Post
Can any one help me with this exercise . . !
I did not understnd it . ?





A number is called a proper divisor of N if M < N and M divides N. A positive integer is called perfect if it is the sum of its positive proper divisors. For example, the positive proper divisors of 28 are 1, 2, 4, 7, and 14 and 1+2+4+7+14=28. Therefore, 28 is perfect. Write a program to display the first 4 perfect integer ..
you did not understand?

It gives a good example.

I think the first perfect integer is 6 because positive proper divisors are 1 2 3 and 1+2+3 = 6.

12 is not perfect cause 1+2+3+4+6 = 16 > 12

I even calculated the numbers from 1 to 20 in my mind, and found none.

You got it?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 34
Reputation: redmaverick is an unknown quantity at this point 
Solved Threads: 0
redmaverick redmaverick is offline Offline
Light Poster
 
0
  #3
Nov 20th, 2009
I think this is a homework problem.....I did the coding and the first 4 perfect numbers are:

The perfect number is 6
The perfect number is 28
The perfect number is 496
The perfect number is 8128


Anyways, I will not give away the solution and rob you of your valuable education, however I will give the pseudo code......hope you can do the rest.....
  1. class name
  2. {
  3. public static void main(String[] args)
  4. {
  5. initialize a count variable to 0 this will help you to count to first 4 perfect numbers
  6.  
  7. use for loop to check first 10000 numbers from which to get the first 4 perfect numbers starting from i=1
  8.  
  9. {
  10. initialize sum variable
  11. use for loop from variable j=1 to check till i
  12.  
  13. {
  14. check divisibility
  15. find sum of j's if i is divisble by j
  16. }
  17.  
  18. use if statement to check for perfect number
  19. and if count is less than 5
  20. {
  21. print the perfect number and increase count by one
  22. }
  23. }
  24.  
  25. }
  26.  
  27. }
  28.  
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 24
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #4
Nov 21st, 2009
I got it ..
I will do it know .. if I can =)

Thanx for yours explanation ..
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 24
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #5
Nov 21st, 2009
  1. package perfectnumbers;
  2. import java.util.Scanner;
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Scanner read = new Scanner(System.in);
  7. int num,count,sum;
  8.  
  9. System.out.printf(" Enter ur number: ");
  10. num = read.nextInt();
  11.  
  12. count=0;
  13. for( int m=1 ; m <10000; m++)
  14. {
  15. sum=0;
  16. for ( int n=1; n<m; m++ )
  17. {
  18. if ( m %n == 0 )
  19. sum = sum + n;
  20. else;
  21. }
  22. if ((num / m) && ( count < 5))
  23. System.out.println (" The perfect number is: ");
  24. else;
  25. }
  26. count ++;
  27. }
  28.  
  29. }
  30. }
  31. }
Last edited by alreem; Nov 21st, 2009 at 6:43 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 24
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
-1
  #6
Nov 21st, 2009
It's not true . !
Last edited by alreem; Nov 21st, 2009 at 6:45 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 34
Reputation: redmaverick is an unknown quantity at this point 
Solved Threads: 0
redmaverick redmaverick is offline Offline
Light Poster
 
0
  #7
Nov 21st, 2009
Originally Posted by alreem View Post
It's not true . !
  1. Scanner read = new Scanner(System.in);
  2.  
  3. System.out.printf(" Enter ur number: ");
  4.  
  5. num = read.nextInt();

There is no need to take input from the user. You are checking for perfect numbers from 1 to 10000.


also to check for perfect number you should use the condition

sum==m (i.e 1+2+3 = = 6)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 24
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #8
Nov 21st, 2009
  1. count=0;
  2. for( int m=1 ; m <10000; m++)
  3. {
  4. sum=0;
  5. for ( int n=1; n<m; m++ )
  6. {
  7. if ( m %n == 0 )
  8. sum = sum + n;
  9. else;
  10. }
  11. if ((num / m) && ( count < 5))
  12. System.out.println (" The perfect number is: ");
  13. else;
  14. }
  15. count ++;
  16. }


like this . . ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 34
Reputation: redmaverick is an unknown quantity at this point 
Solved Threads: 0
redmaverick redmaverick is offline Offline
Light Poster
 
0
  #9
Nov 21st, 2009
try something like this

  1. count=0;
  2. for( int m=1 ; m <10000; m++)
  3. {
  4. sum=0;
  5. for ( int n=1; n<m; m++ )
  6. {
  7. if ( m %n == 0 )
  8. sum = sum + n;
  9. else;
  10. }
  11. if ((sum==m) && ( count < 5))
  12. System.out.println (" The perfect number is: ");
  13. else;
  14. count ++;
  15.  
  16. }
  17.  
  18. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 24
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #10
Nov 22nd, 2009
does not work . . !


it gives me :

the perfect number is:
the perfect number is:
the perfect number is:
the perfect number is:
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 1051 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC