I'm currently making a program that list all the perfect numbers from 1-1000. and list also its factors.

heres my code.

public class perfect
{
public static void main(String[]args)
{
int sum=0;
int x=0;
for(int num=1;num<1000;num++)
 {
           for(int factor=1;factor<num;factor++){
                x=num%factor;
                if(x==0)
                     sum=sum+factor;}
            if(sum==num){    
                System.out.print(num);
                System.out.print("The factors are ");
                for(int factor=1;factor<num;factor++)
                {
                       x=num%factor;
                        if(x==0)
                           System.out.print(factor);
                 }
              }
           }
     }
}

when i compiled it there is no error but it doesn't display anything...

could someone help me...

Dani AI

Generated

Good catch by and the cleaned-up example from shows why your program prints nothing: sum must be reset for each candidate num. Otherwise the divisor totals keep accumulating across numbers and sum == num is never true again after the first few iterations.

If you want this to run faster and list factors neatly, factor each n only up to sqrt(n) and add divisor pairs. Also, perfect numbers greater than 1 are even, so you can skip odds in the 1..1000 range to avoid wasted work. Here is a compact version that prints each perfect number and its proper factors in sorted order:

import java.util.*;

public class PerfectNumbersFast {
  public static void main(String[] args) {
    for (int n = 2; n <= 1000; n++) {
      if ((n & 1) == 1) continue;       // no odd perfect numbers known
      List<Integer> f = properDivisors(n);
      int sum = 0; for (int d : f) sum += d;
      if (sum == n) System.out.println(n + " -> " + f);
    }
  }

  static List<Integer> properDivisors(int n) {
    List<Integer> f = new ArrayList<>();
    f.add(1);                           // 1 is a proper divisor for n > 1
    int r = (int)Math.sqrt(n);
    for (int d = 2; d <= r; d++) {
      if (n % d == 0) {
        f.add(d);
        int q = n / d;
        if (q != d) f.add(q);           // avoid double-adding squares
      }
    }
    Collections.sort(f);
    return f;
  }
}

Sanity check: the output for 1..1000 should be 6, 28, and 496 with the expected factor lists. Small extras: you can early-exit the inner loop if a running sum exceeds n, and remember that 1 is not perfect (its proper-divisor sum is 0). For anyone reading ’s later C++ attempt: watch the stray semicolon after if (num % i == 0) and pass the argument to isPerfect(num).

Recommended Answers

All 6 Replies

I'm currently making a program that list all the perfect numbers from 1-1000. and list also its factors.

heres my code.

public class perfect
{
public static void main(String[]args)
{
int sum=0;
int x=0;
for(int num=1;num<1000;num++)
 {
           for(int factor=1;factor<num;factor++){
                x=num%factor;
                if(x==0)
                     sum=sum+factor;}
            if(sum==num){    
                System.out.print(num);
                System.out.print("The factors are ");
                for(int factor=1;factor<num;factor++)
                {
                       x=num%factor;
                        if(x==0)
                           System.out.print(factor);
                 }
              }
           }
     }
}

when i compiled it there is no error but it doesn't display anything...

could someone help me...

Couple edits:

public class PerfectNumbers
{
public static void main(String[]args)
{
int sum=0, x=0;
for(int num=1;num<1000;num++)
{
for(int factor=1;factor<num;factor++){
x=num%factor;
if(x==0)
sum=sum+factor;}
if(sum==num){
System.out.println("\n\n"+num+":");
System.out.println("\nThe factors are: ");


for(int factor=1;factor<num;factor++)
{
x=num%factor;
if(x==0)
System.out.println(factor);
}
}
sum=0;
}
}
}

displays:

6:

The factors are:
1
2
3


28:

The factors are:
1
2
4
7
14


496:

The factors are:
1
2
4
8
16
31
62
124
248
Press any key to continue...

i didn't run the code but it seems you need to set sum to zero somewhere in your code. (after you complete factoring a number i guess)

thanks for the help!! the gathering!!!

i never thought that i just have to set the sum to zero.

it was really a great help!

I'm writing a program for C++ and I must be missing something. The program doesn't display the perfect numbers and always gets hung-up one the two red sections.

#include <iostream>
using namespace std;
bool isPerfect(int Num);


bool isPerfect(int num)
{
    int sum = 0;

    for (int i = 1; i < num; i++)
    {
        if (num % i == 0);
        {
            sum = sum + i;
        }
    }
    if (sum == num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{
    cout << "The perfect numbers are:" << endl;
    for (int num = 1; num <= 1000; num++)
    {
        if (isPerfect() == 1)
        {
            cout << num << " ";
        }
    }
}

I'm writing a program for C++ and I must be missing something.

Yes, you are missing the fact that this is someone else's two year old thread and it's in Java, not C++.

public class Text extends Thread{
public static void main (String argv[])}
Test b=new Test();
b.start();
}
public void run(){
System.out.println("Running");
}
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.