1.

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* Angewandte Mathematik, SS11
* Problem: 10490 Mr. Azad and his Son!!!!!
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2834
*
* @author Gent Selmanaj
* @author Marco Schuster
* @version 1.0, 2011-03-29
*
* Method :
* Status : Accepted
* Runtime: 0.112
*/
public class Main {

/**
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int in;
in = Integer.parseInt(input.readLine());
while(in!=0) {
//System.out.println("Calculating for "+in);
//pruefen ob die uebergebene zahl prim ist, wenn nicht kann der
rest entfallen...
int teiler=0;
for(int i=1;i<in+1;i++) {
if(in%i==0) {
// System.out.println(i+" teilt "+in);
teiler++;
}
}
if(teiler!=2) {
System.out.println("Given number is NOT prime! NO perfect number
is available.");
in = Integer.parseInt(input.readLine());
continue;
}

long res=(long) (Math.pow(2,(in-1))*(Math.pow(2,in)-1));
long sum=0;

/*
//Teiler berechnen
for(long i=1;i<res;i++) {
if(res%i==0) {
sum+=i;
}
}
*/
//ermittelte Liste aller Primzahlen, die eine perfekte Zahl ergeben.
//Ein Check der Ergebnisse auf die Teilersumme dauert einfach zu lange.
if(in==2 || in==3 || in==5 || in==7 || in==13 || in==17 || in==19 || in==31)
sum=res;
else
sum=0;

if(sum==res) { //perfekte zahl
System.out.println("Perfect: "+res+"!");
} else {
System.out.println("Given number is prime. But, NO perfect number
is available.");
}
in = Integer.parseInt(input.readLine());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}


2.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

/**
* Angewandte Mathematik, SS11
* Problem: 10490 Mr. Azad and his Son!!!!!
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1431
*
* @author Unverzart Michael
* @author Wurth Manuel
* @version 1.0, 3/4/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.148
*/

public class Main {

public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
while((inputLine = reader.readLine()) != null){
int n = new Integer(inputLine);
if (n==0) break;
if(BigInteger.valueOf(n).isProbablePrime(8)) {
long k = (long) (Math.pow(2, n)-1);
if(BigInteger.valueOf(k).isProbablePrime(8)) {
long p = (long) (Math.pow(2, n-1)*k);
System.out.format("Perfect: %d!%n",p) ;
}
else System.out.println("Given number is prime. But, NO perfect number is available.");
} else System.out.println("Given number is NOT prime! NO perfect number is available.");
}
}
}

3.

/**
* Angewandte Mathematik, SS11
* Problem: 10490 Mr. Azad and his Son!!!!!
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1431
*
* @author Markus Schöllner
* @author Andreas Maier
* @version 1.0, 03/29/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.184
*/

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;

do {
number = scanner.nextInt();
if(number == 0) break;
boolean isPrime = true;
boolean isPerfect = false;
long result=0;

for(int c = 2; c < number; c++) {
if(number%c == 0) isPrime = false;
if(isPrime == false)break;
}
if(isPrime){
isPerfect = true;
result = (int) (Math.pow(2, number)-1);
if(number!=31) {
for(int c = 2; c < result; c++) {
if(result%c == 0) isPerfect = false;
if(isPerfect == false)break;
}
}
if(isPerfect) result = result * (int) (Math.pow(2, number-1));
}
if(isPerfect) System.out.println("Perfect: "+result+"!");
else {
if(isPrime) System.out.println("Given number is prime. But, NO perfect number is available.");
else System.out.println("Given number is NOT prime! NO perfect number is available.");
}
}while(number !=0);
}
}

---------------------------------------------------------------

1.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 10490 - Mr. Azad and his Son!!!!!
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1431
*
* @author Evgeni Pavlidis
* @version 1.0, 06/02/2010
*
* Method : Precomputed
* Status : Accepted
* Runtime: 0.108
*/

/* Numbers are found by a brute force generator (c++)

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
long long sum, n, j, squareRoot;
for(int i = 1; i < 32; i++)
{
cout << i+1 << " ";
n = (long long)(2 << (i-1)) * ((long long)(2 << i) - 1);
cout << n << " ";

sum = 1;
squareRoot = (long long)sqrt(n);
for(j = 2; j <= squareRoot; j++)
if(n % j == 0)
sum += j + (n/j);


if(squareRoot * squareRoot == n)
sum -= squareRoot;

cout << sum << " " << (sum == n) << endl;


}
return 0;
}

*/


import java.io.*;
import java.util.*;

public class Main {

private static List<Integer> factors = new ArrayList<Integer>();
private static long sum;


public static void main(String...args)
{
Scanner sc = new Scanner(System.in);
int k;

// see c++ generator
Set<Integer> primes = new HashSet<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
primes.add(11);
primes.add(13);
primes.add(17);
primes.add(19);
primes.add(23);
primes.add(29);
primes.add(31);


while(( k = sc.nextInt()) != 0)
{
if(!primes.contains(k))
System.out.println("Given number is NOT prime! NO perfect number is available.");
else
if(k == 29 || k == 11 || k == 23)
System.out.println("Given number is prime. But, NO perfect number is available.");
else
System.out.println("Perfect: " + ((2l << (k-2))*((2l << (k-1))-1)) + "!");
}
}
}