Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- public class PuntoTres {
- Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) {
- PuntoTres myApp = new PuntoTres();
- myApp.run();
- }
- Stack<Long> divisorsOfN(Long n)
- {
- Stack<Long> ret = new Stack<Long>();
- for(long i = 1; i*i <= n; i++)
- {
- if(n%i == 0)
- {
- ret.push(i);
- if(n/i != i) ret.push(n/i);
- }
- }
- return ret;
- }
- long forceReadLong()
- {
- long n = 0;
- while(true)
- {
- try
- {
- System.out.println("Introduzca el valor de N:");
- n = Long.parseLong(scanner.next());
- return n;
- }
- catch(Exception ex)
- {
- System.out.println("Se introdujo un número inválido, reintente.");
- }
- }
- }
- boolean checkPerfectNumber(Long n, Stack<Long> divisors)
- {
- Long sumOfDivs = 0*1L;
- while(!divisors.isEmpty()) sumOfDivs+=divisors.pop();
- return sumOfDivs-n == n;
- }
- void run()
- {
- Long n = forceReadLong();
- Stack<Long> divisors = divisorsOfN(n);
- System.out.println(checkPerfectNumber(n, divisors) ? "Es número perfecto." : "No es número perfecto.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement