• 0

highest perfect number 100000000 in java


Question

I need a java code that will give me the highest perfect number under 100.000.000

I have done everything I can but I still don't know. I searched the internet but they only go up to 1000.

Help me please

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

post the code that you have done so far and we gladly help you because if you expect people to spoon feed with code then forget it.

Link to comment
Share on other sites

  • 0
package toetsen;

 

public class Toets {

public static int checkNumber(long num)

{

  long sum = 0;

  for(long i = 1; i < num; i++)

  if(num % i == 0)

    sum += i;

  if(sum == num)

  return 1;

  else

  return 0;

}

 

public static long perfectNumbers(int n)

{

  long num = 6;

  while(num <= n)

  {

  if(checkNumber(num) == 1)

    System.out.println(num+" is perfect number");

  num+=2;

  }

  return num;

}

 

public static void main(String[] arg)

{

  perfectNumbers(100000000);

}

}

 

 

 

it should work. but it has to be faster than a minute

Link to comment
Share on other sites

  • 0

Can't you just use the equation found here in a loop which increases "p" until the outcome is greater than 100,000,000 and then take the previous result?

Link to comment
Share on other sites

  • 0
1. Use a loop to count backwards your highest number - n.

2. Use checkNumber() to see if the value is a perfect number

3. If it is, return the value

4. Print result

 

Psuedo code:

 



function findPerfectNumber(n)
  for i = n, counting down to 0
    if checkNumber(i) is true then
      return i
 
function main
  perfectNumber = findPerfectNumber(100000000);
  print "Highest perfect number is " + perfectNumber

  • Like 2
Link to comment
Share on other sites

  • 0

 

1. Use a loop to count backwards your highest number - n.
2. Use checkNumber() to see if the value is a perfect number
3. If it is, return the value
4. Print result
 
Psuedo code:
 
function findPerfectNumber(n)
  for i = n, counting down to 0
    if checkNumber(i) is true then
      return i
 
function main
  perfectNumber = findPerfectNumber(100000000);
  print "Highest perfect number is " + perfectNumber

 

 

this does seem the best approach, since the OP wants the highest perfect number under a given limit; the OPs code does the opposite and takes longer to get and not only that but it collects all the numbers, which isn't the objective.

Link to comment
Share on other sites

  • 0

I'm new to java so I don't know how to code that to java

Then your issue is not how to find the highest perfect number, but how to code in Java. You should look into Java tutorials to learn the basics of the language.

Link to comment
Share on other sites

This topic is now closed to further replies.