Some Big integer problems for beginners

Revision en2, by Sukarna_Paul, 2018-08-20 15:59:21

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++){
      arr[i] = arr[i-1].add(arr[i-2]);
    }
    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();
      sum = sum.add(a);
    }
    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= n.add(BigInteger.valueOf(1));
    }
    n=BigInteger.valueOf(1);
    BigInteger ans2=BigInteger.valueOf(1);
    for(int i=0;i<20;i++){
        ans2=ans2.multiply(n);
        n= n.add(BigInteger.valueOf(1));
    }
    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.

Tags big integers, #java, large number, #for_beginners

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Sukarna_Paul 2018-08-20 15:59:21 2 Tiny change: ' Problem 15](https://' -> ' Problem 16](https://'
en1 English Sukarna_Paul 2018-08-20 15:58:10 5517 Initial revision (published)