• 0

highest perfect number 100000000 in java


Question

9 answers to this question

Recommended Posts

  • 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

  • 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
  • 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.

This topic is now closed to further replies.