Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

optional #1

wants to merge 1 commit into from

Conversation

NanNor
Copy link
Owner

@NanNor NanNor commented Aug 4, 2016

perfect number solution

public class PerfectNumber {
public static void main(String[] args) {
perfect(10000);
}

//print all perfect numbers from 1 to n
public static void perfect(int n){
for (int i = 1; i<n; i++){
    int comp = 0;
    for (int j = 1; j<i; j++){
        if (i%j == 0)
            comp += j;  
    }
    if (comp == i)
        System.out.println(i);
    }
}

//returns true or false if n is an "imperfect number"
//also includes perfect numbers
public static boolean imPerfect(int n){
    int comp = 0;
    for (int i = 1; i<n; i++){
        if (n%i == 0)
            comp += i;  
    }
    return (comp == n || comp == n+1 || comp == n-1 || comp == n+2 || comp == n-2);
}

}

perfect number solution 


public class PerfectNumber {
	public static void main(String[] args) {
		perfect(10000);
	}
	
	//print all perfect numbers from 1 to n
	public static void perfect(int n){
	for (int i = 1; i<n; i++){
		int comp = 0;
		for (int j = 1; j<i; j++){
			if (i%j == 0)
				comp += j;	
		}
		if (comp == i)
			System.out.println(i);
		}
	}
	
	//returns true or false if n is an "imperfect number"
	//also includes perfect numbers
	public static boolean imPerfect(int n){
		int comp = 0;
		for (int i = 1; i<n; i++){
			if (n%i == 0)
				comp += i;	
		}
		return (comp == n || comp == n+1 || comp == n-1 || comp == n+2 || comp == n-2);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant