This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding perfect numbers using predicate methods  RSS feed

 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write a predicate method isPerfect(x) that returns true if the integer x is perfect, and false otherwise. I need to test this implementation by writing a main program that uses the isPerfect method to check for perfect numbers in the range 1 to 9999 by testing each number in turn. Whenever it identifies a perfect number, program should display that number on the screen. The first two lines of output should be 6 and 28.

I have tried to combine isPrime metohd with this program, but for some reason I am getting bunch of numbers from 1 to 9999. I don't know whether my thought process is bad or I might just overlooked something.

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line doesn't look correct.

You may want to include a definition of a perfect number.
 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself)

After looking at definition of a perfect number I've modified code, so that it looks like this, but again I am getting the wrong results.

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the wrong results. 

How are you debugging the code?
I suggest that you call the method with only a few values (not 1000) and look at the results of each expression to see where the code is going wrong.  I use print statements to print out the values of all the variables as there values are changed and used.  If you know what the values should be, seeing the actual values from the program will show you where the problem is.
 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it guys!!! Woo hoo


   
   

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job!  If you want to see a few comment on your code, read on.

1) Goob job using constants for HIGH_NUM and LOW_NUM
2) Classes should start with an uppercase letter, so the class should be named PerfectNumber2.
3) In small programs like this, it doesn't matter much, but in general you should avoid putting if statements on one line.  So for instance,
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue your isPrime method is not correct.  It will return false for the input value of "2" - which IS prime.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why include x in the loop if you are just going to subtract it later?

 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: Good job!

Thanks! It means a lot to a new programmer. I will adopt your advises.

fred rosenberger wrote:I would argue your isPrime method is not correct. It will return false for the input value of "2"

I will just write one more if statement. 

Fred Kleinschmidt wrote:Why include x in the loop if you are just going to subtract it later? 

I've already did what you suggested, just I haven't posted here. But thanks anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!