Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years. ×

### Sukarna_Paul's blog

By Sukarna_Paul, history, 18 months ago, ,

Some Big integer problems for beginners Solving BigInteger Problem is fun. There are many problem solvers around the world who come to use Java BigInteger class though they do not code in Java usually. Here are some simple problems with their links and solution. This problem are chosen from different online judges. Try yourself before you see the solution.

Codeforces Gym : 112

# Solution

import java.util.Scanner ;
import java.math.BigInteger;
public class Main{
public static void main(String[] args){
int a,b;
Scanner input = new Scanner(System.in);
a = input.nextInt();
b = input.nextInt();
BigInteger A = BigInteger.valueOf(a);
BigInteger B= BigInteger.valueOf(b);
System.out.printf("%d",A.pow(b).subtract(B.pow(a)));

}
}


Uva 10183 — How Many Fibs?

# Solution

import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
BigInteger arr[] = new BigInteger[50000];
arr[0]= BigInteger.valueOf(1);
arr[1]= BigInteger.valueOf(2);
for(int i=2;i<50000;i++){
}
while(true){

BigInteger a,b;
a = input.nextBigInteger();
b = input.nextBigInteger();
if(b.compareTo(BigInteger.valueOf(0))==0){
break;
}
int count=0;
for(int i=0;i<50000;i++){
if(arr[i].compareTo(a)>=0 && arr[i].compareTo(b)<=0){
count++;
}
if(arr[i].compareTo(b)>0){
break;
}
}
System.out.println(count);
}
}
}


URI 1279

# Solution

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
boolean start = true;
while(input.hasNext()){
boolean leap_year = false;
boolean ordinary = true;
if(start == false){
System.out.print("\n");
}
start = false;
BigInteger year;
year = input.nextBigInteger();
BigInteger four;
four = BigInteger.valueOf(4);
BigInteger fourh;
fourh = BigInteger.valueOf(400);
BigInteger oneh;
oneh = BigInteger.valueOf(100);
BigInteger temp = year.remainder(four);
if(temp.compareTo(BigInteger.valueOf(0))==0){
temp = year.remainder(oneh);
if(temp.compareTo(BigInteger.valueOf(0))==0){
temp = year.remainder(fourh);
if(temp.compareTo(BigInteger.valueOf(0))==0){
System.out.println("This is leap year.");
leap_year = true;
ordinary = false;
}
}
else{
System.out.println("This is leap year.");
leap_year = true;
ordinary = false;
}
}
temp = year.remainder(BigInteger.valueOf(15));
if(temp.compareTo(BigInteger.valueOf(0))==0){
System.out.println("This is huluculu festival year.");
ordinary = false;
}
if(leap_year==true){
temp = year.remainder(BigInteger.valueOf(55));
if(temp.compareTo(BigInteger.valueOf(0))==0){
System.out.println("This is bulukulu festival year.");
ordinary = false;
}
}
if(ordinary == true){
System.out.println("This is an ordinary year.");
}
}
}
}


Project Euler Problem 13

# Solution

import java.util.*;
import java.math.*;

public class problem13{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
BigInteger sum, a;
sum = BigInteger.valueOf(0);
for(int i=0;i<100;i++){
a = input.nextBigInteger();
}
System.out.println(sum);    //take the first 10 digits manually
}
}


Project Euler Problem 15

# Solution

import java.math.*;

public class Problem15{
public static void main(String[] args){
BigInteger n=BigInteger.valueOf(1);
BigInteger ans=BigInteger.valueOf(1);
for(int i=0;i<40;i++){
ans=ans.multiply(n);
}
n=BigInteger.valueOf(1);
BigInteger ans2=BigInteger.valueOf(1);
for(int i=0;i<20;i++){
ans2=ans2.multiply(n);
}
System.out.println(ans.divide(ans2.multiply(ans2)));
}
}


Project Euler Problem 16

# Solution

import java.math.*;

public class Problem16{
public static void main(String[] args){
//BigInteger n=BigInteger.valueOf(1000);
BigInteger two = BigInteger.valueOf(2);
BigInteger ans = two.pow(1000);
String s = ""+ans;
System.out.println(s);
int sum=0;
for(int i=0;i<s.length();i++){
sum+=s.charAt(i)-'0';
}
System.out.println(sum);
}
}


Thank you. Happy coding.

• +1

 » 18 months ago, # | ← Rev. 4 →   +2 Python versionproblem 1 : a,b = map(int,input().split()) print((a**b)-(b**a)) problem 2 : import bisect array = [1,2] for i in range(2,500) : array.append(array[i-1]+array[i-2]) while True : a,b = map(int,input().split()) if a==0 and b==0 : break ans = bisect.bisect_right(array,b) - bisect.bisect_left(array,a) print(ans) 
•  » » 18 months ago, # ^ |   0 This is even faster than my Java code , your one took 124ms while mine took 140ms. Thanks for sharing.
 » 13 months ago, # |   0 In the second problem (How Many Fibs)No need to make an array of 50000 fibs, only 500 would be enoughSince if you gave a and b the values of 1 and 10^100, the answer would be 480 meaning 480 fibs between 1 and 10^100 only
 » 12 months ago, # |   0 can you provide these code in c++?