• 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

    • Ah well, who needs computers in the next 10... 15 years right? At this point is just money laundering.
    • RollerCoaster Tycoon 3 and Voidwrought are free on the Epic Games Store by Pulasthi Ariyasinghe PC gamers can now jump in and grab two more games to keep this week, as the Epic Games Store's ever-present giveaway promotion has just gone through another refresh. Replacing last week's Citizen Sleeper and Robobeat offers, the store has brought in RollerCoaster Tycoon 3 Complete Edition and Voidwrought to keep. From the duo, Roller Coaster Tycoon 3: Complete Edition is an enhanced version of the classic title that was originally released in 2004. It comes with enhancements such as widescreen and 1080p resolution support, as well as increased compatibility with modern PC hardware. It also comes bundled with all the extra content from the Soaked! and Wild! expansion packs. "Control park finances, shops, services and staff to succeed in dozens of scenarios," reads the description. "Become a true tycoon and embark on your promising new career, or create your ideal park without money woes in sandbox mode. Satisfy your guests’ needs and keep your park running smoothly to succeed." If management is not your style, Voidwrought lands as a 2D action-platformer featuring hand-drawn cosmic horrors. The title touts tight platforming and close‑quarters combat, all brought together with a strong emphasis on mobility. "Descend below the star-scorched surface and explore the multidimensional depths below," says the studio Powersnake about the game. "Witness the corrupted revelry of the Court, lose yourself in the icy tunnels of the Old Waters, and discover the grim fate of the Abandoned Expedition." The RollerCoaster Tycoon 3 Complete Edition and Voidwrought giveaways are set to run until June 25 on the Epic Games Store, giving PC gamers seven days to claim the latest offer. Once this closes out, new freebies will take their place on the same day as always. Don't forget that mobile gamers can check out the Epic Game Store's weekly giveaways on Android and iOS to grab a freebie there as well.
    • I have a feeling this memory shortage issues are going to linger 6-8 years so until and unless Chinese memory floods the markets we are doomed
  • Recent Achievements

    • First Post
      kinowa earned a badge
      First Post
    • Rookie
      krychek57 went up a rank
      Rookie
    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
    • Dedicated
      Scoobystu earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      442
    2. 2
      +Edouard
      169
    3. 3
      PsYcHoKiLLa
      134
    4. 4
      Xenon
      77
    5. 5
      Michael Scrip
      75
  • Tell a friend

    Love Neowin? Tell a friend!