arrays - Java: Please explain this code in detail for me. -


this question related post: https://stackoverflow.com/a/35810542/5888956

basically, have write program reads in integer keyboard, , has able display 50! using array. (cannot use biginteger or anything) got far, previous post.

import java.util.scanner; class factorial {     public static void main(string[] args)     {         int n;          scanner kb = new scanner(system.in);         system.out.println("enter n");         n = kb.nextint();         int [] result = fact(n);          int = result.length-1;         while (i > 0 && result[i] == 0)         {             i--;          }          system.out.print(n + "! = ");         while (i >= 0)         {             system.out.print(result[i--]);         }          system.out.println();     }      public static int[] fact(int n)     {          int[] = new int[100];         a[0] = 1;            (int = 1; <= n; i++)         {             int carry = 0;             for(int j = 0; j < a.length; j++)             {                 int x = a[j] * + carry;                 a[j] = x % 10;                 carry = x / 10;                          }          }          return a;     } } 

but can't still understand logic behind here. especially, part,

for (int = 1; <= n; i++) {       int carry = 0;       for(int j = 0; j < a.length; j++)       {            int x = a[j] * + carry;            a[j] = x % 10;            carry = x / 10;                    } } 

i'm trying solve pen , paper, can understand completely. (of course small number 4!) if type 4 n, 4! 24 (4*3*2*1). in logic, when i 1 a[0] 1 because initialized above, after loop ends once, become 0?

i = 1, j = 0 x = a[0] * 1 + 0 = 1 a[0] = 0 carry = 1 // ------repeat loop = 2, j = 0 x = a[0] * 1 + 1 = 1 a[0] = 0 carry = 1 

so apparently not right logic, think. can please please me understand this?

the factorial operation means multiplying whole bunch of numbers, conceptually, not difficult operation implement. reason why becomes problem when implementing in code, produces huge numbers, in fact, large held in 1 int (4 bytes) or long (8 bytes) variables (12! max hold in int). otherwise, like:

int fact = 1; for(int = 2 ; <= n ; i++) {     fact *= i; } 

so 1 way deal treat number "array of digits", in base 10. example, number 2016 treated array: int[] digits = {2, 0, 1, 6}, way of saying 2016 = 2*1000 + 0*100 + 1*10 + 6*1.

now let's imagine have 8-bits computers, , can't represent numbers larger 255 (or pow(2,8)-1). wouldn't able 2016*2 directly, but, because multiplication distributive a(b+c) = ab+ac, decompose multiplication of "large" number 2016 smaller multiplications so:
2016 → {2, 0, 1, 6} → 2*1000 + 0*100 + 1*10 + 6
2016 * 2 = (2*1000 + 0 + 1*10 + 6) * 2 = (2*2)*1000 + (2*0) + (2*1)*10 + (2*6)

now can these small multiplications hand:
2016 * 2 → {2, 0, 1, 6} * 2 → {2*2, 0*2, 1*2, 6*2} → {4, 0, 2, 12}

we 12 first digit, need carry on 1:
{4, 0, 2, 12} → {4, 0, 2+1, 2} → {4, 0, 3, 2} = 4032

that's code does:

a[0] = 1; (int = 1; <= n; i++) {     int carry = 0;     for(int j = 0; j < a.length; j++) {         int x = a[j] * + carry;         a[j] = x % 10;         carry = x / 10;                  } } 

it takes number or "array of digits" a, , multiplies digits a[j] individually ix = a[j] * i. if it's bigger 9, take carry next digit → carry = x / 10, , you're left remnant value of digit → a[j] = x % 10.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -