Skip to main content

Find Factorial Of N (N- As big as you can think)

YouTube channel: https://www.youtube.com/channel/UC6U6yckyKymb36I0ytwrnXw

Want to find factorial of N without using built-in BIG Number classes available in Java.
Here you go! Achieve it with the simple idea of Linked Lists in Java.

File: BIGNumbers.java
---------------------------------------

//Data class to represent BIG number each data node containing digits
//This helps handle big numbers as List can grow.
//int and long types support limited digits only (Range: 2^32 , 2^64 )
//A better way to handle big numbers is to use an array of bytes / list of bytes
class Data{
byte value;//Byte type is sufficient to save value 0~9
Data nextData;
Data(byte value){
this.value = value;
this.nextData=null;
}
}

public class BIGNumbers{
byte zero=0;
byte carryForward=0;
byte ten=10;
Data result=null;
Data resultData=null;

BIGNumbers(){}

//To find list size
int listSize(Data list){
int length=0;
while(list!=null){
length++;
list=list.nextData;
}
return length;
}

Data add(Data L1, Data L2){
        if(L1==null && L2==null){
            return null;
        }
        add(L1.nextData,L2.nextData); //recursive makes it easier go to the last node and start summing up
        byte value = (byte) (L1.value + L2.value + carryForward);
        carryForward=0;
        if(value>=10){
            carryForward =1; //maximum carrry forward value can be 1 only
            value = (byte) (value%ten); //get value excluding carry forward
        }
        Data d = new Data(value);
        if(result==null){
            result =d;
        }else{
            d.nextData = result;
            result = d;
        }
        return result;
    }

public Data sum(Data L1,Data L2){
int L1Size = listSize(L1);
int L2Size = listSize(L2);

//Check if the two lists are of same size
//And make them even in size
/*ex:
If list 1 => 1 2 3 4 5
   list 2 => 999
 
   L1Size = 5
   L2Size = 3 ( Add data nodes padding two 0 data )
 
If list 1=> 9 9 9
  list 2=> 1 2 3 4 5

  L1Size = 3 ( Add data nodes padding two 0 data )
  L2Size = 5
*
*/

if(L1Size>L2Size){
int fill=(L1Size-L2Size);
while(fill>0){
Data dat = new Data(zero);
                dat.nextData=L2;
                L2=dat;  //Swap
                fill--;
}
}

if(L2Size>L1Size){
int fill=(L2Size-L1Size);
while(fill>0){
Data dat=new Data(zero);
dat.nextData=L1;
L1=dat;
fill--;
}
}

resultData=add(L1,L2);
     
   if(carryForward!=0){
        Data dat=new Data(carryForward);
        dat.nextData=resultData;
        resultData = dat;
       }
return resultData;
}

void printData(){
        if(resultData!=null){
Data tmp=resultData;
        while(tmp!=null){
            System.out.print(tmp.value);
            tmp=tmp.nextData;
        }}else{
        System.out.println("List Empty");
        }
    }

void printData(Data list){
        if(list!=null){
Data tmp=list;
        while(tmp!=null){
            System.out.print(tmp.value);
            tmp=tmp.nextData;
        }}else{
        System.out.println("List Empty");
        }
    }

void reset(){
result = null;
carryForward=0;
}

Data f(int i){
Data num1=new Data((byte) 0);
Data num2=new Data((byte) 1);
BIGNumbers e=new BIGNumbers();
Data nthFeb=null;
if(i==0 || i==1){
return new Data((byte)i);
}else{
while(i>1){
nthFeb=e.sum(num1, num2);
num1 = num2;
num2 = nthFeb;
e.reset();
i--;
}
}
return nthFeb;
}
}

File: Main.java
---------------------------------

public class Main {

public static void main(String[] args){
BIGNumbers f=new BIGNumbers();
Data nthFeb=f.f(9999);
f.printData(nthFeb);
}

}

Output:
------------
20793608237133498072112648988642836825087036094015903119682945866528501423455686648927456034305226515591757343297190158010624794267250973176133810179902738038231789748346235556483191431591924532394420028067810320408724414693462849062668387083308048250920654493340878733226377580847446324873797603734794648258113858631550404081017260381202919943892370942852601647398213554479081823593715429566945149312993664846779090437799284773675379284270660175134664833266377698642012106891355791141872776934080803504956794094648292880566056364718187662668970758537383352677420835574155945658542003634765324541006121012446785689171494803262408602693091211601973938229446636049901531963286159699077880427720289235539329671877182915643419079186525118678856821600897520171070499437657067342400871083908811800976259727431820539554256869460815355918458253398234382360435762759823179896116748424269545924633204614137992850814352018738480923581553988990897151469406131695614497783720743461373756218685106856826090696339815490921253714537241866911604250597353747823733268178182198509240226955826416016690084749816072843582488613184829905383150180047844353751554201573833105521980998123833253261228689824051777846588461079790807828367132384798451794011076569057522158680378961532160858387223882974380483931929541222100800313580688585002598879566463221427820448492565073106595808837401648996423563386109782045634122467872921845606409174360635618216883812562321664442822952537577492715365321134204530686742435454505103269768144370118494906390254934942358904031509877369722437053383165360388595116980245927935225901537634925654872380877183008301074569444002426436414756905094535072804764684492105680024739914490555904391369218696387092918189246157103450387050229300603241611410707453960080170928277951834763216705242485820801423866526633816082921442883095463259080471819329201710147828025221385656340207489796317663278872207607791034431700112753558813478888727503825389066823098683355695718137867882982111710796422706778536913192342733364556727928018953989153106047379741280794091639429908796650294603536651238230626

Comments

  1. This is definitely a useful blog for all the newbies who want to learn it.

    Team Best for Your Home http://www.bestforyourhome.co.in/

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Interface

Problem Statement A Java interface can only contain method signatures and fields. Interface can be used to achieve polymorphism. In this problem you will practice your knowledge on interfaces. You are given an interface   AdvancedArithmetic   which contains a method signature   public abstract int divisorSum(int n) . You need to write a class called MyCalculator which implements the interface. divisorSum   function just takes an integer as input and return the sum of all its divisors. For example divisors of 6 are 1,2,3 and 6, so   divisorSum   should return 12. Value of n will be at most 1000. Read the partially completed code in the editor and complete it. You just need to write the MyCalculator class only.   Your class shouldn't be public. Sample Input 6 Sample Output I implemented: AdvancedArithmetic 12 Explanation Divisors of 6 are 1,2,3 and 6. 1+2+3+6=12. import java.util.*; interface AdvancedArithmetic{   public abstract int divisorSum(int n

Problem: Java Exception Handling

Problem Statement Create a class myCalculator which consists of a single method power(int,int). This method takes two integers,   n   and   p , as parameters and finds   n p . If either   n   or   p   is negative, then the method must throw an exception which says "n and p should be non-negative". Please read the partially completed code in the editor and complete it. Your code mustn't be public. No need to worry about constraints, there won't be any overflow if your code is correct. Sample Input 3 5 2 4 -1 -2 -1 3 Sample Output 243 16 java.lang.Exception: n and p should be non-negative java.lang.Exception: n and p should be non-negative import  java.util.*; class myCalculator{     int power(int n,int p) throws java.lang.Exception{         int power=1;         while(p>0){             power=power*(n);             p--;         }                 if(n==0) power=0;                if(n<0 | p<0){       

Google Cloud Shell | Delete instance