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

CODILITY: Determine whether given string of parentheses is properly nested.

Task description A string S consisting of N characters is called  properly nested  if: S is empty; S has the form " (U) " where U is a properly nested string; S has the form " VW " where V and W are properly nested strings. For example, string " (()(())()) " is properly nested but string " ()) " isn't. Write a function: class Solution { public int solution(String S); } that, given a string S consisting of N characters, returns 1 if string S is properly nested and 0 otherwise. For example, given S = " (()(())()) ", the function should return 1 and given S = " ()) ", the function should return 0, as explained above. Assume that: N is an integer within the range [ 0 .. 1,000,000 ]; string S consists only of the characters " ( " and/or " ) ". Complexity: expected worst-case time complexity is O(N); expected worst-case space complexity is O(1) (not counting the storage requi...

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{   p...

Distinct: Compute number of distinct values in an array.

Task description Write a function class Solution { public int solution(int[] A); } that, given a zero-indexed array A consisting of N integers, returns the number of distinct values in array A. Assume that: N is an integer within the range [ 0 .. 100,000 ]; each element of array A is an integer within the range [ −1,000,000 .. 1,000,000 ]. For example, given array A consisting of six elements such that: A[0] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1 the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3. Complexity: expected worst-case time complexity is O(N*log(N)); expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). Elements of input arrays can be modified. class Solution { public int solution ( int [] A) { // write your code in Java SE 8 int len=A.length; int count= 1 ; ...