Programming Interest Group http://www.comp.hkbu.edu.hk/~chxw/pig/index.htm - PowerPoint PPT Presentation

About This Presentation
Title:

Programming Interest Group http://www.comp.hkbu.edu.hk/~chxw/pig/index.htm

Description:

Programming Interest Group http://www.comp.hkbu.edu.hk/~chxw/pig/index.htm Tutorial Four High-Precision Arithmetic * – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 37
Provided by: Xia124
Category:
Tags: chxw | comp | edu | group | hkbu | htm | http | index | interest | pig | programming | www

less

Transcript and Presenter's Notes

Title: Programming Interest Group http://www.comp.hkbu.edu.hk/~chxw/pig/index.htm


1
Programming Interest Grouphttp//www.comp.hkbu.ed
u.hk/chxw/pig/index.htm
  • Tutorial Four
  • High-Precision Arithmetic

2
Machine Arithmetic
  • 32-bit machine
  • An integer is roughly in the range 231
    2,147,483,648
  • 64-bit machine
  • An integer is roughly in the range 263
    9,223,372,036,854,775,808
  • Unsigned integer can double the upper limit
  • Sometimes we need to operate on very large
    integers, especially in the area of cryptography
  • High-precision integers, or multiple precision
    integers

3
Integer Libraries
  • C
  • ltstdlib.hgt, ltmath.hgt
  • C
  • GNU C Integer class
  • http//www.chemie.fu-berlin.de/chemnet/use/info/li
    bgpp/libgpp_20.html
  • Java
  • BigInteger class in java.math
  • http//java.sun.com/j2se/1.4.2/docs/api/java/math/
    BigInteger.html

4
Java BigInteger Example
  • Factorial n (usually written n!) is the product
    of all integers up to and including n (1x 2 x 3 x
    ... x n).

import java.math.BigInteger public class
Factorial public static void main(String
args) //-- BigInteger solution.
BigInteger n BigInteger.ONE for (int i1
ilt20 i) n n.multiply(BigInteger.valueOf(
i)) System.out.println(i "! " n)
//-- int solution (BAD IDEA BECAUSE ONLY
WORKS TO 12). int fact 1 for (int
i1 ilt20 i) fact fact i
System.out.println(i "! " fact)

5
Java BigInteger Example (Cont.)
1! 1 2! 2 3! 6 4! 24 5! 120 6!
720 7! 5040 8! 40320 9! 362880 10!
3628800 11! 39916800 12! 479001600 13!
6227020800 14! 87178291200 15!
1307674368000 16! 20922789888000 17!
35568742809600018! 640237370572800019!
121645100408832000 20! 2432902008176640000
1! 1 2! 2 3! 6 4! 24 5! 120 6!
720 7! 5040 8! 40320 9! 362880 10!
3628800 11! 39916800 12! 479001600 13!
1932053504 BAD 14! 1278945280 BAD 15!
2004310016 BAD 16! 2004189184 BAD 17!
-288522240 BAD 18! -898433024 BAD 19!
109641728 BAD 20! -2102132736 BAD
6
What are we doing now?
  • As a CS student, its not enough to just know how
    to use those high-precision integer libraries.
  • Its better to know how to design and implement a
    high-precision integer arithmetic library.
  • One student from our department has done this as
    his Honours Project.
  • Addition, subtraction, multiplication, and
    division, modular, exponentiation, etc

7
Review of Number Systems
  • Positional notation using base b (or radix b) is
    defined by the rule(a3a2a1a0.a-1a-2)b
    a3b3a2b2a1b1a0a-1b-1a-2b-2
  • Binary number system b 2
  • 2 digits 0 and 1
  • Decimal number system b 10
  • 10 digits 0, 1, 2, 3, , 9
  • Hexadecimal number system b 16
  • 16 digits 0, 1, 2, , 9, A, B, C, D, E, F

Radix Point
8
High-precision Integers
  • How to represent enormous integers?
  • Arrays of digits
  • The initial element of the array represents the
    least significant digits
  • An integer 1234567890 is stored as an array0,
    9, 8, 7, 6, 5, 4, 3, 2, 1
  • Maintain a counter for the number of digits
  • Maintain the sign of the integer positive or
    negative?
  • Linked list of digits
  • It can support real arbitrary precision
    arithmetic
  • But
  • Waste of memory (each node takes a pointer)
  • The performance is not as good as arrays

9
An Example Using Array
http//www.comp.hkbu.edu.hk/chxw/pig/code/bignum.
c define MAXDIGITS 100 /maximum length bignum
/ define PLUS 1 / positive sign bit
/ define MINUS -1 / negative sign bit
/ typedef struct char digitsMAXDIGITS /
represent the number / int signbit / PLUS or
MINUS / int lastdigit / index of high-order
digit / bignum
  • Remarks
  • Each digit (0-9) is represented using a
    single-byte character.
  • In fact, using higher numerical bases (e.g., 64,
    128, 256) is more efficient.

10
Print a Bignum
void print_bignum(bignum nPtr) int i if
(nPtr-gtsignbit MINUS) printf(-) for (i
nPtr-gtlastdigit i gt 0 i--) printf(c,
0nPtr-gtdigitsi) printf(\n)
11
Convert an Integer to Bignum
int_to_bignum(int s, bignum nPtr) int
i / counter / int t / int to work
with / if (s gt 0) nPtr-gtsignbit PLUS else
nPtr-gtsignbit MINUS for (i0 iltMAXDIGITS
i) nPtr-gtdigitsi (char)
0 nPtr-gtlastdigit -1 t abs(s) while (t
gt 0) nPtr-gtlastdigit nPtr-gtdigits
nPtr-gtlastdigit (t 10) t t /
10 if (s 0) nPtr-gtlastdigit 0
12
Initialize a Bignum
  • initialize_bignum(bignum nPtr)
  • int_to_bignum(0,nPtr)
  • int max(int a, int b)
  • if (a gt b) return(a)
  • else return(b)

13
Compare Two Bignums
compare_bignum(bignum a, bignum b) int
i / counter / if ((a-gtsignbit MINUS)
(b-gtsignbit PLUS)) return(PLUS) if
((a-gtsignbit PLUS) (b-gtsignbit MINUS))
return(MINUS) if (b-gtlastdigit gt a-gtlastdigit)
return (PLUS a-gtsignbit) if (a-gtlastdigit gt
b-gtlastdigit) return (MINUS a-gtsignbit) for
(i a-gtlastdigit igt0 i--) if
(a-gtdigitsi gt b-gtdigitsi) return(MINUS
a-gtsignbit) if (b-gtdigitsi gt a-gtdigitsi)
return(PLUS a-gtsignbit) return(0)
14
How to avoid leading zeros?
zero_justify(bignum n) while ((n-gtlastdigit gt
0) (n-gtdigits n-gtlastdigit
0)) n-gtlastdigit -- if ((n-gtlastdigit 0)
(n-gtdigits0 0)) n-gtsignbit PLUS /
hack to avoid -0 /
15
Addition of Two Bignums
add_bignum(bignum a, bignum b, bignum
c) int carry / carry digit / int
i / counter / initialize_bignum(c) if
(a-gtsignbit b-gtsignbit) c-gtsignbit
a-gtsignbit else if (a-gtsignbit MINUS)
a-gtsignbit PLUS subtract_bignum(b,a,c)
a-gtsignbit MINUS else
b-gtsignbit PLUS
subtract_bignum(a,b,c)
b-gtsignbit MINUS return
16
Addition of Two Bignums (cont.)
c-gtlastdigit max(a-gtlastdigit,b-gtlastdigit)1
carry 0 for (i0 ilt(c-gtlastdigit) i)
c-gtdigitsi (char) (carrya-gtdigitsib-gtd
igitsi) 10 carry (carry a-gtdigitsi
b-gtdigitsi) / 10 zero_justify(c)
17
Subtraction of Two Bignums
subtract_bignum(bignum a, bignum b, bignum
c) int borrow / has anything been
borrowed? / int v / placeholder digit
/ int i / counter / initialize_bignum(c)
if ((a-gtsignbit MINUS) (b-gtsignbit
MINUS)) b-gtsignbit -1
b-gtsignbit add_bignum(a,b,c)
b-gtsignbit -1
b-gtsignbit return if
(compare_bignum(a,b) PLUS) subtract_bignum(
b,a,c) c-gtsignbit MINUS return
18
Subtraction of Two Bignums (Cont.)
c-gtlastdigit max(a-gtlastdigit,b-gtlastdig
it) borrow 0 for (i0
ilt(c-gtlastdigit) i) v (a-gtdigitsi -
borrow - b-gtdigitsi) if (a-gtdigitsi gt
0) borrow 0 if (v lt 0) v v
10 borrow 1
c-gtdigitsi (char) v 10
zero_justify(c)
19
Digit Shift
/ E.g., shift 123456 by 2 will get 12345600
In memory 6,5,4,3,2,1 ? 0,0,6,5,4,3,2,1 /
digit_shift(bignum nPtr, int d) / multiply
nPtr by 10d / int i / counter / if
((nPtr-gtlastdigit 0) (nPtr-gtdigits0
0)) return for (inPtr-gtlastdigit igt0
i--) nPtr-gtdigitsid nPtr-gtdigitsi for
(i0 iltd i) nPtr-gtdigitsi
0 nPtr-gtlastdigit nPtr-gtlastdigit d
20
Multiplication of Two Bignums
multiply_bignum(bignum a, bignum b, bignum
c) bignum row / represent shifted row
/ bignum tmp / placeholder bignum / int i,
j / counters / initialize_bignum(c) row
a for (i0 i lt b-gtlastdigit i) for
(j1 j lt b-gtdigitsi j) add_bignum(c,r
ow,tmp) c tmp digit_shift(row,1)
c-gtsignbit a-gtsignbit b-gtsignbit zero_
justify(c)
21
Division of Two Bignums
divide_bignum(bignum a, bignum b, bignum
c) bignum row /
represent shifted row / bignum tmp
/ placeholder bignum / int asign,
bsign / temporary signs / int i, j
/ counters / initialize_bignum(c)
c-gtsignbit a-gtsignbit b-gtsignbit asign
a-gtsignbit bsign b-gtsignbit a-gtsignbit
PLUS b-gtsignbit PLUS
22
Division of Two Bignums (Cont.)
initialize_bignum(row) initialize_bignum(tmp)
c-gtlastdigit a-gtlastdigit for
(ia-gtlastdigit i gt 0 i--) digit_shift(row
,1) row.digits0 a-gtdigitsi c-gtdigitsi
0 while (compare_bignum(row,b) ! PLUS)
c-gtdigitsi subtract_bignum(row,b,t
mp) row tmp zero_justify(c) a-gt
signbit asign b-gtsignbit bsign
23
For more information
  • Donald E. Knuth The Art of Computer Programming,
    Volume 2.
  • Seminumerical Algorithms, Chapter 4
  • Arithmetic
  • Positional number systems
  • Floating point arithmetic
  • Multiple precision arithmetic
  • Radix conversion
  • Rational arithmetic
  • Polynomial arithmetic
  • Manipulation of power series

24
Practice I Primary Arithmetic
  • http//acm.uva.es/p/v100/10035.html
  • Children are taught to add multi-digit numbers
    from right-to-left one digit at a time. Many find
    the "carry" operation - in which a 1 is carried
    from one digit position to be added to the next -
    to be a significant challenge. Your job is to
    count the number of carry operations for each of
    a set of addition problems so that educators may
    assess their difficulty.
  • Input
  • Each line of input contains two unsigned integers
    less than 10 digits. The last line of input
    contains 0 0.
  • Output
  • For each line of input except the last you should
    compute and print the number of carry operations
    that would result from adding the two numbers, in
    the format shown below.

25
Practice I Primary Arithmetic
  • Sample Input
  • 123 456
  • 555 555
  • 123 594
  • 0 0
  • Sample Output
  • No carry operation.
  • 3 carry operations.
  • 1 carry operation.

Hint Use an array of characters to store the
digits of an integer
26
Practice II Reverse and Add
  • http//acm.uva.es/p/v100/10018.html
  • The "reverse and add" method is simple choose a
    number, reverse its digits and add it to the
    original. If the sum is not a palindrome (which
    means, it is not the same number from left to
    right and right to left), repeat this procedure.
  • For example 195 Initial number 591 ----- 786
    687 ----- 1473 3741 ----- 5214 4125 -----
    9339 Resulting palindrome

In this particular case the palindrome 9339
appeared after the 4th addition. This method
leads to palindromes in a few step for almost all
of the integers. But there are interesting
exceptions. 196 is the first number for which no
palindrome has been found. It is not proven
though, that there is no such a palindrome.
27
Practice II Reverse and Add
  • Task You must write a program that gives the
    resulting palindrome and the number of iterations
    (additions) to compute the palindrome.
  • You might assume that all tests data on this
    problem - will have an answer , - will be
    computable with less than 1000 iterations
    (additions), - will yield a palindrome that is
    not greater than 4,294,967,295.  
  • The Input
  • The first line will have a number N with the
    number of test cases, the next N lines will have
    a number P to compute its palindrome.
  • The Output
  • For each of the N tests you will have to write a
    line with the following data minimum number of
    iterations (additions) to get to the palindrome
    and the resulting palindrome itself separated by
    one space.

28
Practice II Reverse and Add
  • Sample Input
  • 3
  • 195
  • 265
  • 750
  • Sample Output
  • 4 9339 5 45254 3 6666

29
Practice III Archeologists Dilemma
  • http//acm.uva.es/p/v7/701.html
  • An archeologist seeking proof of the presence of
    extraterrestrials in the Earth's past, stumbles
    upon a partially destroyed wall containing
    strange chains of numbers.
  • The left-hand part of these lines of digits is
    always intact, but unfortunately the right-hand
    one is often lost by erosion of the stone.
    However, she notices that all the numbers with
    all its digits intact are powers of 2, so that
    the hypothesis that all of them are powers of 2
    is obvious.
  • To reinforce her belief, she selects a list of
    numbers on which it is apparent that the number
    of legible digits is strictly smaller than the
    number of lost ones, and asks you to find the
    smallest power of 2 (if any) whose first digits
    coincide with those of the list.
  • Thus you must write a program such that given an
    integer, it determines (if it exists) the
    smallest exponent E such that the first digits of
    2E coincide with the integer (remember that more
    than half of the digits are missing).

30
Practice III Archeologists Dilemma  
  • Input 
  • It is a set of lines with a positive integer N
    not bigger than 2147483648 in each of them.
  • Output 
  • For every one of these integers a line containing
    the smallest positive integer E such that the
    first digits of 2E are precisely the digits of N,
    or, if there is no one, the sentence no power
    of 2".

Sample Output 7 (2, 4, 8, 16, 32, 64,
12827)8 (2, 4, 8, 16, 32, 64, 128,
25628)20 (2, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384,
32768, 65536, 131072, 262144,
524288, 1048576220)
Sample Input 1210
31
Analysis of the problem
  • The legible digits form a number N
  • Assume there are k missing digits
  • The we have the following inequalities
  • Nx10k lt 2E lt (N1)x10k
  • We perform the lg() operation
  • lg(Nx10k) lt lg(2E) lt lg ((N1)x10k), which leads
    to
  • lgN klg10 lt E lt lg(N1) klg10
  • We can search for the k such that, there is an
    integer in the middle

32
SampleSolution
void process(int n) int k, e double d1,
d2, d3, d4 double left, right d4
log(2) d3 log(10) / d4 d1 log(n) / d4
d2 log(n1) / d4 k length(n)1
left d1kd3 right d2kd3 e
(int)ceil(left) while(e gt right)
k left d1kd3 right d2kd3
e (int)ceil(left) printf("d\n", e)
include ltstdio.hgt include ltstdlib.hgt include
ltmath.hgt void process(int) int
length(int) int main() int in while(
scanf("u", in) ! EOF) process(in)
return 0
int length(int n) int len 0 if(n lt 10)
return 1 while(n gt 0) n n/10
len return len
33
Practice IV How Many Fibs?
  • http//acm.uva.es/p/v101/10183.html
  • Recall the definition of the Fibonacci numbers
  • f1 1, f2 2, fn fn-1fn-2 (n gt 3)
  • Given two numbers a and b, calculate how many
    Fibonacci numbers are in the range a,b.

34
Practice IV How Many Fibs?
  • Input Specification
  • The input contains several test cases. Each test
    case consists of two non-negative integer numbers
    a and b. Input is terminated by ab0. Otherwise,
    altblt10100. The numbers a and b are given with
    no superfluous leading zeros.
  • Output Specification
  • For each test case output on a single line the
    number of Fibonacci numbers fi with altfiltb.

35
Practice IV How Many Fibs?
  • Sample Input
  • 10 100
  • 1234567890 9876543210
  • 0 0
  • Sample Output
  • 5
  • 4

Hint Use the bignum functions!
36
More practice on basic arithmetic
  • http//acm.uva.es/p/v8/847.html
  • http//acm.uva.es/p/v100/10077.html
  • http//acm.uva.es/p/v101/10105.html
  • http//acm.uva.es/p/v101/10127.html
  • http//acm.uva.es/p/v102/10202.html
Write a Comment
User Comments (0)
About PowerShow.com