Integer.toString(int)
public class ... { public static void main(String[] args) { int n_given = new Random() .nextInt(1000); System.out.println("n_given's digit d0: " + digit(n_given, 0)); System.out.println("n_given's digit d1: " + digit(n_given, 1)); System.out.println("n_given's digit d2: " + digit(n_given, 2)); }
public static int digit(int n_given, int place) { return n_given / Math.pow(10, place) % 10; } }
The all-or-none, two-equilibrium arrangements [of electronic components] are [best]. [Then] it is natural to use a system of arithmetic in which the digits are also two-valued. "First Draft of a &Report on the EDVAC [Electronic Discrete Variable Automatic Computer]"
void base_2_expansion(int n, int[] b) { // resulting bits stored in |b[]| // i.e. this code will set |b[]| // to be {...,1,0,1,1,0,0,...} // as appropriate // let |ngiven| denote the initial given value for e int k = 0; while ( n > 0 ) { // at this point n == Lngiven/2nR b[k] = n % 2; n = n/2; // in Java, division of |int|s automatically does floor // so this statement |n = n/2;| resets n // to: |_|_ngiven / 2k_| / 2_| // which is: |_ngiven / 2(k+1)_| k = k + 1; // then with k changed here to k + 1, // n == |_ngiven / 2k_| for the new k } // assume |b[]| otherwise contains zeroes }
until n is 0, repeatedly do the following: * write 1 if n is odd or 0 if n is even * divide n by 2, discarding any fractional part or remainder the result is the REVERSE of the 1s and 0s that you wrote
* write all the powers of two until reach (or pass) n * for each power of two 2k here, going from large down to small: + if 2k > n, write a 0 + otherwise, write a 1 and subtract p from n the result is the sequence of 1s and 0s in the order written
int exponentiation(int a, int e) { // a * a * a * ... * a e times int result = 1; while ( e-- > 0 ) result *= a; return result; }
$ man ssh-keygen
sqrrep := a; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2
result := 1; sqrrep := a; // now sqrrep is a1 which is a20 sqrrep := sqrrep * sqrrep; // now sqrrep is a2 which is a21 sqrrep := sqrrep * sqrrep; // now sqrrep is a4 which is a22 · · · sqrrep := sqrrep * sqrrep; // now sqrrep is a8192 which is a213 result := result * sqrrep; // now result is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 sqrrep := sqrrep * sqrrep; // now sqrrep is a which is a2 result := result * sqrrep;
result * sqrrep
result
sqrrep
int exponentiation(int a, int e) { int result = 1; int sqrrep = a; while ( e > 0 ) { // e == |_egiven / 2i_| and sqrrep == a2i if ( e % 2 == 1 ) // as above with binary representation // this condition corresponds // to egiven containing 2i result = result * sqrrep; // so make |result| contain sqrrep == a2i sqrrep = sqrrep * sqrrep; // advance sqrrep to a2(i+1) e = e/2; // advance e to |_egiven/2(i+1)_| } return result; }
e % 2 == 1
int modular_exponentiation(int a, int e, int m) { int result = 1; int sqrrep = a ; while ( e > 0 ) { if ( e % 2 == 1 ) result = result * sqrrep ; sqrrep = sqrrep * sqrrep ; e = e/2; // e := |_e/2_| } return result; }
int modular_exponentiation( int a, int e, int m // a := , e := , m := ) { int result = 1; // result := int sqrrep = a % m; // sqrrep := % := while ( e > 0 ) { // > 0 := if ( e % 2 == 1 ) // % 2 == 1 := == 1 := result = result * sqrrep % m; // result := * % // := % // := sqrrep = sqrrep * sqrrep % m; // sqrrep := * % // := % // := e = e/2; // e := |_/2_| := |__| := } while ( e > 0 ) { // > 0 := if ( e % 2 == 1 ) // % 2 == 1 := == 1 := result = result * sqrrep % m; // result := * % // := % // := sqrrep = sqrrep * sqrrep % m; // sqrrep := * % // := % // := e = e/2; // e := |_/2_| := |__| := } while ( e > 0 ) { // > 0 := if ( e % 2 == 1 ) // % 2 == 1 := == 1 := result = result * sqrrep % m; sqrrep = sqrrep * sqrrep % m; // sqrrep := * % // := % // := e = e/2; // e := |_/2_| := |__| := } while ( e > 0 ) { // > 0 := if ( e % 2 == 1 ) // % 2 == 1 := == 1 := result = result * sqrrep % m; // result := * % // := % // := sqrrep = sqrrep * sqrrep % m; // sqrrep := * % // := % // := e = e/2; // e := |_/2_| := |__| := } while ( e > 0 ) { // > 0 := if ( e % 2 == 1 ) // % 2 == 1 := == 1 := result = result * sqrrep % m; // result := * % // := % // := sqrrep = sqrrep * sqrrep % m; // sqrrep := * % // := % // := e = e/2; // e := |_/2_| := |__| := } while ( e > 0 ) { // > 0 := if ( e % 2 == 1 ) // % 2 == 1 := == 1 := result = result * sqrrep % m; sqrrep = sqrrep * sqrrep % m; // sqrrep := * % // := % // := e = e/2; // e := |_/2_| := |__| := } while ( e > 0 ) { // > 0 := if ( e % 2 == 1 ) // % 2 == 1 := == 1 := result = result * sqrrep % m; // result := * % // := % // := sqrrep = sqrrep * sqrrep % m; // sqrrep := * % // := % // := e = e/2; // e := |_/2_| := |__| := } while ( e > 0 ) // > 0 := return result; // return }