Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:50:23+00:00 2026-06-06T22:50:23+00:00

How can I use a for loop to find the first 3 perfect numbers

  • 0

How can I use a “for” loop to find the first 3 perfect numbers after 28?
Here is some code I’m using.

I can’t seem to get anything past 2 numbers. If I try to increase i<= 2000000000 it tells me the integer is to large.

public class JBaneling
{
    public static void main(String args[])
    {
        System.out.println("3 nearest Perfect numbers after 29 ");

        for (int i = 29; i <= 2000000000; i++) {
            test1(i);
        }
    }



    public static void test1(int number)
    {
        int sum = 0;

        for(int divisor=1; divisor < number; divisor++)
        {
            if ((number % divisor) ==0)
            {
                sum = sum + divisor;
            }
        }

        if(sum==number)
        {
            System.out.println(number + " is a perfect number");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:50:24+00:00Added an answer on June 6, 2026 at 10:50 pm

    It’s just taking a long time to compute the third perfect number. After 8128 the next perfect number is 33550336. Now consider that your code is testing every integer between 8128 ... 33550336, by computing each divisor of each of those integers.

    Also consider that this next perfect number, 33550336 is the largest perfect number that you’ll be able to represent with a java int: Integer.MAX_VALUE is (2^31 – 1). You’ll get a few more with a long – Long.MAX_VALUE is (2^63 – 1) – but you’ll be waiting a long time for that 8th perfect number.

    Note that you only need to consider divisors up to the square root of number, then find their matching divisor above the square root, but even so you’ll still need to wait a really long time before getting to the next perfect number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: // Import io so we can use file objects import
In PHP I can use a foreach loop such that I have access to
In Oracle, we can use FOR employee_rec in (select id from mytbl) LOOP DBMS_OUTPUT.PUT_LINE(employee_rec.id);
How can I use a batch file to loop through non-integer values? I'm trying
So I do iteration with for loop and I can't use result of result_array()
I can use the following to get the contents of a folder with no
I am having some problems running a find loop inside of a subroutine when
I use $.ajax to read xml info in for loop here is my xml
I am trying to use a .each loop to find specific elements in a
My first question here, after enjoying lots og other peoples questions and answers. Thanks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.