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

    • Usually the bigger ones with many fixes/changes take a few, theyre an exception to the rule most likely
    • If you don’t get lucky with Valve’s Steam Machine reservation system, you can make your own Steam Machine instead. Valve says that “starting with the SteamOS 3.8 release, you can put together your own Steam Machine using whatever PC parts you want.” SteamOS 3.8.10 launched last week with a slew of updates, including “improved compatibility with recent Intel and AMD platforms.” Alongside that improved compatibility, Valve is giving gamers the green light to install SteamOS on their own desktops. In an interview with The Verge, Valve’s Pierre-Loup Griffais said Valve has been “rolling out improvements to [SteamOS] so it’s more compatible with desktop hardware,” including eventual support for Nvidia graphics. Griffais says Valve has “a growing team” working on Nvidia driver support for SteamOS, adding, “We’re collaborating with Nvidia very closely.” While he mentioned that Nvidia support might not come this year, Griffais emphasized that “it’s certainly something that we’re working on in the background.”     Subscription not needed: https://archive.fo/Tssfc Subscription needed: https://www.theverge.com/games/953411/valve-steamos-desktop-nvidia
    • I thought they were supposed to reduce the amount of restarts needed to install these updates? It took 3 restarts to install this update...
    • I know an idiot at work who is "Director for innovation projects" and inevitably has been tasked with following up on this whole trend of "AI and its work applicability". He recently sent out a survey on the topic, for the workforce to disclose their usage and attitudes towards AI. The survey was badly, sloppily typed out, the questions and answers (when multiple choices) were loosely correlated and, all and all, it would already be miserable in the times of MS Office Autocorrect alone. Then, I had AI responding to it all (on the open-ended questions) with beautiful, to-the-point prose after I provided it with minor but proper guidance, taking me under 10 minutes. All this to say that I am sure that if AI comes to replace someone, it will still be me, and the other idiot will still stay.
    • Man, the memories.
  • Recent Achievements

    • Dedicated
      HidekoYamamoto94 earned a badge
      Dedicated
    • One Month Later
      timbobit earned a badge
      One Month Later
    • One Month Later
      nates earned a badge
      One Month Later
    • Week One Done
      Almohandis earned a badge
      Week One Done
    • Rookie
      dorf went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      454
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      107
    4. 4
      Michael Scrip
      84
    5. 5
      Steven P.
      70
  • Tell a friend

    Love Neowin? Tell a friend!