Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PerfectNumbers {
- public static void main(String[] args) {
- long start = System.currentTimeMillis();
- int goal = 0;
- int amount = 5;
- int i = 2;
- System.out.print("The first " + amount + " perfect numbers: ");
- while (goal < amount) {
- if (i == sumOfDivisors(i)) {
- System.out.print(i + " ");
- goal++;
- }
- i++;
- }
- System.out.println("");
- long finish = System.currentTimeMillis();
- System.out.println("Time of completion " + (finish - start) + " ms");
- }
- public static double sumOfDivisors(int x) {
- int result = 0;
- int stop = 0;// It returns 0 if the amount is greater than the number of divisors
- for (int i = 1; i < x/2; i++) {
- if (x % i == 0) {
- result += i;
- if (result > x) {
- return stop;
- }
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement