------------------------------------------------------------ well, remembering back to | elementary school (really old review, eh? ;-) | , ------------------------------------------------------------ digits are | {0,1,2,...,8,9} — up to but not including our base, ten | ------------------------------------------------------------ i.e. 397_ten == | 3*10`2 + 9*10`1 + 7*10`0` | ------------------------------------------------------------ e.g. for 397_ten, d_0 = | 7 | , d_1 = | 9 | , d_2 = | 3 | ------------------------------------------------------------ return n_given / Math.pow(10, place) % 10; ------------------------------------------------------------ := L397/ | 1 | R mod 10 ------------------------------------------------------------ := L | 397 | R mod 10 ------------------------------------------------------------ := | 7 | which is d_0 ------------------------------------------------------------ := L397/ | 10 | R mod 10 ------------------------------------------------------------ := L | 39.7 | R mod 10 ------------------------------------------------------------ := | 39 | mod 10 ------------------------------------------------------------ := | 9 | which is d_1 ------------------------------------------------------------ := L397/ | 100 | R mod 10 ------------------------------------------------------------ := L | 3.97 | R mod 10 ------------------------------------------------------------ := | 3 | mod 10 ------------------------------------------------------------ := | 3 | which is d_2 ------------------------------------------------------------ sounds silly; do you do anything like that? | YES: hours:minutes:seconds | ------------------------------------------------------------ base-eight representation called | octal -- "oct" means 8 as with "octopus" | ------------------------------------------------------------ for base eight, digits are | {0,1,2,...,6,7} -- up to but not including the base | ------------------------------------------------------------ == | 64 | _ten + | 48 | _ten + | 2 | _ten ------------------------------------------------------------ == | 114 | _ten ------------------------------------------------------------ and Computer Science also uses hexadecimal which is base | sixteen | ------------------------------------------------------------ fundamental base Computer Science uses is | two | ------------------------------------------------------------ this was an insight of | John von Neumann | in 1945: ------------------------------------------------------------ base-two arithmetic called | "binary" -- "bi" means 2 as with "bilingual" | ------------------------------------------------------------ for base two, digits are only | {0,...,1} -- up to but not including the base | ------------------------------------------------------------ | "bit" | abbreviates "binary digit" ------------------------------------------------------------ == | 4 | _ten + | 2 | _ten + | 0 | _ten ------------------------------------------------------------ == | 6 | _ten ------------------------------------------------------------ (reversing: | 6 | _ten == 110_two) ------------------------------------------------------------ == | 64 | _ten + ------------------------------------------------------------ + | 8 | _ten + + | 2 | _ten + | 1 | _ten ------------------------------------------------------------ == | 75 | _ten ------------------------------------------------------------ e.g. for 1001011_two, b_0 = | 1 | , b_1 = | 1 | , b_2 = | 0 | , ------------------------------------------------------------ b_3 = | 1 | , b_4 = | 0 | , b_5 = | 0 | , b_6 = | 1 | ------------------------------------------------------------ := L75/ | 1 | R mod 2 ------------------------------------------------------------ := L | 75 | R mod 2 ------------------------------------------------------------ := | 1 | which is b_0 ------------------------------------------------------------ := L75/ | 2 | R mod 2 ------------------------------------------------------------ := L | 37.5 | R mod 2 ------------------------------------------------------------ := | 37 | mod 2 ------------------------------------------------------------ := | 1 | which is b_1 ------------------------------------------------------------ := L75/ | 4 | R mod 2 ------------------------------------------------------------ := L | 18.75 | R mod 2 ------------------------------------------------------------ := | 18 | mod 2 ------------------------------------------------------------ := | 0 | which is b_2 ------------------------------------------------------------ := L | 18 | / 2R mod 2 ------------------------------------------------------------ := L | 9 | R mod 2 ------------------------------------------------------------ := | 9 | mod 2 ------------------------------------------------------------ := | 1 | which is b_3 ------------------------------------------------------------ := L | 9 | / 2R mod 2 ------------------------------------------------------------ := | 4 | mod 2 ------------------------------------------------------------ := | 0 | which is b_4 ------------------------------------------------------------ := L | 4 | / 2R mod 2 ------------------------------------------------------------ := | 2 | mod 2 ------------------------------------------------------------ := | 0 | which is b_5 ------------------------------------------------------------ := L | 2 | / 2R mod 2 ------------------------------------------------------------ := | 1 | mod 2 ------------------------------------------------------------ := | 1 | which is b_6 ------------------------------------------------------------ 75 1 37 1 18 0 9 1 4 0 2 0 1 1 0 result: 1001011 ------------------------------------------------------------ e.g. for | ___ | : ------------------------------------------------------------ result: ------------------------------------------------------------ powers of two: 1, 2, 4, 8, 16, 32, 64, (128) stop (128 > n so write a 0 --- actually inconsequential) 64 <= 75 so write 1 and subtract 64: 75 - 64 := 11 32 > 11 so write 0 16 > 11 so write 0 8 <= 11 so write 1 and subtract 8: 11 - 8 := 3 4 > 3 so write 0 2 <= 3 so write 1 and subtract 2: 3 - 2 := 1 1 <= 1 so write 1 and subtract 1: 1 - 1 := 0 result: 1001011 ------------------------------------------------------------ e.g. for | ___ | : ------------------------------------------------------------ result: ------------------------------------------------------------ // a * a * a * ... * a e times int result = 1; while ( e-- > 0 ) result *= a; return result; ------------------------------------------------------------ one gigahertz means one billion operations per second ------------------------------------------------------------ loop (shown below) will repeat only | approximately 1000 | times(!) ------------------------------------------------------------ // now sqrrep is a` | 1 | which is a2 | 0 | ------------------------------------------------------------ // now sqrrep is a | 2 | which is a2 | 1 | ------------------------------------------------------------ // now sqrrep is a | 4 | which is a2 | 2 | ------------------------------------------------------------ // now sqrrep is a | 8 | which is a2 | 3 | ------------------------------------------------------------ // now sqrrep is a | 16 | which is a2 | 4 | ------------------------------------------------------------ // now sqrrep is a | 32 | which is a2 | 5 | ------------------------------------------------------------ // now sqrrep is a | 64 | which is a2 | 6 | ------------------------------------------------------------ // now sqrrep is a | 128 | which is a2 | 7 | ------------------------------------------------------------ // now sqrrep is a | 256 | which is a2 | 8 | ------------------------------------------------------------ // now sqrrep is a | 512 | which is a2 | 9 | ------------------------------------------------------------ // now sqrrep is a | 1024 | which is a2 | 10 | ------------------------------------------------------------ // now sqrrep is a | 2048 | which is a2 | 11 | ------------------------------------------------------------ // now sqrrep is a | 4096 | which is a2 | 12 | ------------------------------------------------------------ // now sqrrep is a | 8192 | which is a2 | 13 | ------------------------------------------------------------ at that point have a`e — doing only | 14 | operations, not 8,192(!) ------------------------------------------------------------ | as follows, do the preceding and store a`8192, and then continue to 131072: | ------------------------------------------------------------ // now result is a | 8192 | which is a2 | 13 | ------------------------------------------------------------ // now sqrrep is a | 16384 | which is a2 | 14 | ------------------------------------------------------------ // now sqrrep is a | 32768 | which is a2 | 15 | ------------------------------------------------------------ // now sqrrep is a | 65536 | which is a2 | 16 | ------------------------------------------------------------ // now sqrrep is a | 131072 | which is a2 | 17 | ------------------------------------------------------------ which equals a`( | 8192+131072 | ) ------------------------------------------------------------ (which is a`( | 2`13 + 2`17 | )`) ------------------------------------------------------------ int sqrrep = a | % m | ; ------------------------------------------------------------ result = result * sqrrep | % m | ; ------------------------------------------------------------ sqrrep = sqrrep * sqrrep | % m | ; ------------------------------------------------------------ don't straightforwardly start 708`91 (:= | 2.2543765059e+259 | ) ------------------------------------------------------------ int a, int e, int m // a := | 708 | , e := | 91 | , m := | 10001 | ------------------------------------------------------------ int result = 1; // result := | 1 | ------------------------------------------------------------ int sqrrep = a % m; // sqrrep := | 708 | % | 10001 | := | 708 | ------------------------------------------------------------ while ( e > 0 ) { // | 91 | > 0 := | true | ------------------------------------------------------------ if ( e % 2 == 1 ) // | 91 | % 2 == 1 := | 1 | == 1 := | true | ------------------------------------------------------------ // result := | 1 | * | 708 | % | 10001 | ------------------------------------------------------------ // := | 708 | % | 10001 | ------------------------------------------------------------ // := | 708 | ------------------------------------------------------------ // sqrrep := | 708 | * | 708 | % | 10001 | ------------------------------------------------------------ // := | 501264 | % | 10001 | ------------------------------------------------------------ // := | 1214 | ------------------------------------------------------------ e = e/2; // e := L | 91 | /2R := L | 45.5 | R := | 45 | ------------------------------------------------------------ while ( e > 0 ) { // | 45 | > 0 := | true | ------------------------------------------------------------ if ( e % 2 == 1 ) // | 45 | % 2 == 1 := | 1 | == 1 := | true | ------------------------------------------------------------ // result := | 708 | * | 1214 | % | 10001 | ------------------------------------------------------------ // := | 859512 | % | 10001 | ------------------------------------------------------------ // := | 9427 | ------------------------------------------------------------ // sqrrep := | 1214 | * | 1214 | % | 10001 | ------------------------------------------------------------ // := | 14737964 | % | 10001 | ------------------------------------------------------------ // := | 3649 | ------------------------------------------------------------ e = e/2; // e := L | 45 | /2R := L | 22.5 | R := | 22 | ------------------------------------------------------------ while ( e > 0 ) { // | 22 | > 0 := | true | ------------------------------------------------------------ if ( e % 2 == 1 ) // | 22 | % 2 == 1 := | 0 | == 1 := | false | ------------------------------------------------------------ // sqrrep := | 3649 | * | 3649 | % | 10001 | ------------------------------------------------------------ // := | 13315201 | % | 10001 | ------------------------------------------------------------ // := | 3870 | ------------------------------------------------------------ e = e/2; // e := L | 22 | /2R := L | 11 | R := | 11 | ------------------------------------------------------------ while ( e > 0 ) { // | 11 | > 0 := | true | ------------------------------------------------------------ if ( e % 2 == 1 ) // | 11 | % 2 == 1 := | 1 | == 1 := | true | ------------------------------------------------------------ // result := | 9427 | * | 3870 | % | 10001 | ------------------------------------------------------------ // := | 36482490 | % | 10001 | ------------------------------------------------------------ // := | 8843 | ------------------------------------------------------------ // sqrrep := | 3870 | * | 3870 | % | 10001 | ------------------------------------------------------------ // := | 14976900 | % | 10001 | ------------------------------------------------------------ // := | 5403 | ------------------------------------------------------------ e = e/2; // e := L | 11 | /2R := L | 5.5 | R := | 5 | ------------------------------------------------------------ while ( e > 0 ) { // | 5 | > 0 := | true | ------------------------------------------------------------ if ( e % 2 == 1 ) // | 5 | % 2 == 1 := | 1 | == 1 := | true | ------------------------------------------------------------ // result := | 8843 | * | 5403 | % | 10001 | ------------------------------------------------------------ // := | 47778729 | % | 10001 | ------------------------------------------------------------ // := | 3952 | ------------------------------------------------------------ // sqrrep := | 5403 | * | 5403 | % | 10001 | ------------------------------------------------------------ // := | 29192409 | % | 10001 | ------------------------------------------------------------ // := | 9491 | ------------------------------------------------------------ e = e/2; // e := L | 5 | /2R := L | 2.5 | R := | 2 | ------------------------------------------------------------ while ( e > 0 ) { // | 2 | > 0 := | true | ------------------------------------------------------------ if ( e % 2 == 1 ) // | 2 | % 2 == 1 := | 0 | == 1 := | false | ------------------------------------------------------------ // sqrrep := | 9491 | * | 9491 | % | 10001 | ------------------------------------------------------------ // := | 90079081 | % | 10001 | ------------------------------------------------------------ // := | 74 | ------------------------------------------------------------ e = e/2; // e := L | 2 | /2R := L | 1 | R := | 1 | ------------------------------------------------------------ while ( e > 0 ) { // | 1 | > 0 := | true | ------------------------------------------------------------ if ( e % 2 == 1 ) // | 1 | % 2 == 1 := | 1 | == 1 := | true | ------------------------------------------------------------ // result := | 3952 | * | 74 | % | 10001 | ------------------------------------------------------------ // := | 292448 | % | 10001 | ------------------------------------------------------------ // := | 2419 | ------------------------------------------------------------ // sqrrep := | 74 | * | 74 | % | 10001 | ------------------------------------------------------------ // := | 5476 | % | 10001 | ------------------------------------------------------------ // := | 5476 | ------------------------------------------------------------ e = e/2; // e := L | 1 | /2R := L | 0.5 | R := | 0 | ------------------------------------------------------------ while ( e > 0 ) // | 0 | > 0 := | false | ------------------------------------------------------------ return result; // return | 2419 | ------------------------------------------------------------ | 708 | ------------------------------------------------------------ the receiver needs to know | 4627 | ------------------------------------------------------------ ====================================================================


(Copyright © 2009 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)