• 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

    • Look who's back!
    • I wonder how driving laws around the world will change. No way to really tell if people are using phone. Same with smart watches i guess even now and those silly built in tablets for controlling the car instead of buttons.
    • They found a better aligned evil overlord for WhatsApp...
    • Google Chrome 149.0.7827.197 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | Direct Link | 131.0 MB Download: Google Chrome Offline Installer 32-bit | Direct Link | 119.0 MB Download page: Google Chrome Portable Download: Chrome ARM64 | Direct Link View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • (I know it's just an image and also not the point at all, but it really bugs me that the two halves of the necklace don't really fit together... 😅)
  • Recent Achievements

    • Rookie
      DaviKar went up a rank
      Rookie
    • 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
  • Popular Contributors

    1. 1
      +primortal
      463
    2. 2
      +Edouard
      161
    3. 3
      PsYcHoKiLLa
      112
    4. 4
      Michael Scrip
      85
    5. 5
      Steven P.
      70
  • Tell a friend

    Love Neowin? Tell a friend!