Pages

Showing posts with label BigInteger. Show all posts
Showing posts with label BigInteger. Show all posts

1/19/2014

UVA 495 Solution

import java.math.BigInteger;
import java.util.Scanner;

class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger [] bg = new BigInteger[5001];

bg[0] = BigInteger.ZERO;
bg[1] = BigInteger.ONE;
for (int i=2; i<5001; i++) bg[i] = bg[i-1].add(bg[i-2]);
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int i = sc.nextInt();
System.out.println( "The Fibonacci number for "  + i + " is " +  bg[i]);
}
}

}

UVA 10929 Solution

import java.math.BigInteger;
import java.util.Scanner;
class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
StringBuffer br = new StringBuffer("");
BigInteger bg11 = BigInteger.valueOf(11);
boolean flag = false;
while(true) {
String m = sc.nextLine();
if (m.equalsIgnoreCase("0")) break;
BigInteger bg = new BigInteger(m);
if(bg.compareTo(BigInteger.ZERO)==0) break;
int x =(bg.remainder(bg11)).compareTo(BigInteger.ZERO);
//System.out.println(x);
if(x==0){
br.append(m + " is a multiple of 11.\n");
//System.out.println(bg + " is not a multiple of 11.");
}
else {
br.append(m + " is not a multiple of 11.\n");
}
}
System.out.print(br);
}

}