Computations with large numbers

Similar documents
CHAPTER 4 RADICAL EXPRESSIONS

MATH 371 Homework assignment 1 August 29, 2013

å 1 13 Practice Final Examination Solutions - = CS109 Dec 5, 2018

Exercises for Square-Congruence Modulo n ver 11

1 Onto functions and bijections Applications to Counting

Sebastián Martín Ruiz. Applications of Smarandache Function, and Prime and Coprime Functions

A tighter lower bound on the circuit size of the hardest Boolean functions

The Primitive Idempotents in

18.413: Error Correcting Codes Lab March 2, Lecture 8

COMPUTERISED ALGEBRA USED TO CALCULATE X n COST AND SOME COSTS FROM CONVERSIONS OF P-BASE SYSTEM WITH REFERENCES OF P-ADIC NUMBERS FROM

PTAS for Bin-Packing

2. Independence and Bernoulli Trials

Neville Robbins Mathematics Department, San Francisco State University, San Francisco, CA (Submitted August 2002-Final Revision December 2002)

Patterns of Continued Fractions with a Positive Integer as a Gap

Non-uniform Turán-type problems

Introducing Sieve of Eratosthenes as a Theorem

arxiv:math/ v1 [math.gm] 8 Dec 2005

Some identities involving the partial sum of q-binomial coefficients

Can we take the Mysticism Out of the Pearson Coefficient of Linear Correlation?

Mu Sequences/Series Solutions National Convention 2014

means the first term, a2 means the term, etc. Infinite Sequences: follow the same pattern forever.

For combinatorial problems we might need to generate all permutations, combinations, or subsets of a set.

The Selection Problem - Variable Size Decrease/Conquer (Practice with algorithm analysis)

Part 4b Asymptotic Results for MRR2 using PRESS. Recall that the PRESS statistic is a special type of cross validation procedure (see Allen (1971))

Ideal multigrades with trigonometric coefficients

Arithmetic Mean and Geometric Mean

2SLS Estimates ECON In this case, begin with the assumption that E[ i

v 1 -periodic 2-exponents of SU(2 e ) and SU(2 e + 1)

Laboratory I.10 It All Adds Up

Evaluating Polynomials

About k-perfect numbers

Application of Generating Functions to the Theory of Success Runs

On the introductory notes on Artin s Conjecture

1. A real number x is represented approximately by , and we are told that the relative error is 0.1 %. What is x? Note: There are two answers.

Multiple Choice Test. Chapter Adequacy of Models for Regression

Factorization of Finite Abelian Groups

hp calculators HP 30S Statistics Averages and Standard Deviations Average and Standard Deviation Practice Finding Averages and Standard Deviations

Bayes (Naïve or not) Classifiers: Generative Approach

Math 10 Discrete Mathematics

Chapter 5 Properties of a Random Sample

Introduction to local (nonparametric) density estimation. methods

Dr. Shalabh Department of Mathematics and Statistics Indian Institute of Technology Kanpur

The internal structure of natural numbers, one method for the definition of large prime numbers, and a factorization test

Quantum Plain and Carry Look-Ahead Adders

{ }{ ( )} (, ) = ( ) ( ) ( ) Chapter 14 Exercises in Sampling Theory. Exercise 1 (Simple random sampling): Solution:

Econometric Methods. Review of Estimation

Entropy ISSN by MDPI

The Mathematical Appendix

F. Inequalities. HKAL Pure Mathematics. 進佳數學團隊 Dr. Herbert Lam 林康榮博士. [Solution] Example Basic properties

On the convergence of derivatives of Bernstein approximation

Extend the Borel-Cantelli Lemma to Sequences of. Non-Independent Random Variables

Pseudo-random Functions

Modified Cosine Similarity Measure between Intuitionistic Fuzzy Sets

A Primer on Summation Notation George H Olson, Ph. D. Doctoral Program in Educational Leadership Appalachian State University Spring 2010

Centroids & Moments of Inertia of Beam Sections

Parallel Programming: Speedups and Amdahl s law. Definition of Speedup

Semi-Riemann Metric on. the Tangent Bundle and its Index

Estimation of Stress- Strength Reliability model using finite mixture of exponential distributions

Assignment 5/MATH 247/Winter Due: Friday, February 19 in class (!) (answers will be posted right after class)

Solving Constrained Flow-Shop Scheduling. Problems with Three Machines

Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand DIS 10b

The Strong Goldbach Conjecture: Proof for All Even Integers Greater than 362

Functions of Random Variables

The number of observed cases The number of parameters. ith case of the dichotomous dependent variable. the ith case of the jth parameter

Two Fuzzy Probability Measures

f f... f 1 n n (ii) Median : It is the value of the middle-most observation(s).

Summary of the lecture in Biostatistics

ESS Line Fitting

(b) By independence, the probability that the string 1011 is received correctly is

best estimate (mean) for X uncertainty or error in the measurement (systematic, random or statistical) best

UNIT 2 SOLUTION OF ALGEBRAIC AND TRANSCENDENTAL EQUATIONS

Pseudo-random Functions. PRG vs PRF

#A27 INTEGERS 13 (2013) SOME WEIGHTED SUMS OF PRODUCTS OF LUCAS SEQUENCES

Minimizing Total Completion Time in a Flow-shop Scheduling Problems with a Single Server

Algorithms Theory, Solution for Assignment 2

IS 709/809: Computational Methods in IS Research. Simple Markovian Queueing Model

UNIVERSITY OF OSLO DEPARTMENT OF ECONOMICS

UNIT 7 RANK CORRELATION

Maps on Triangular Matrix Algebras

Introduction to Probability

Unimodality Tests for Global Optimization of Single Variable Functions Using Statistical Methods

QR Factorization and Singular Value Decomposition COS 323

On the Rational Valued Characters Table of the

7.0 Equality Contraints: Lagrange Multipliers

Algorithms Design & Analysis. Hash Tables

Mean is only appropriate for interval or ratio scales, not ordinal or nominal.

PATTERNS IN NUMBER THEORY Theophanes E. Raptis Computational Applications Group Division of Applied Technologies NCSR Demokritos 2015

02/15/04 INTERESTING FINITE AND INFINITE PRODUCTS FROM SIMPLE ALGEBRAIC IDENTITIES

STK4011 and STK9011 Autumn 2016

arxiv: v1 [math.st] 24 Oct 2016

Feature Selection: Part 2. 1 Greedy Algorithms (continued from the last lecture)

Test Paper-II. 1. If sin θ + cos θ = m and sec θ + cosec θ = n, then (a) 2n = m (n 2 1) (b) 2m = n (m 2 1) (c) 2n = m (m 2 1) (d) none of these

A Markov Chain Competition Model

CHAPTER 6. d. With success = observation greater than 10, x = # of successes = 4, and

Fibonacci Identities as Binomial Sums

Johns Hopkins University Department of Biostatistics Math Review for Introductory Courses

Chapter 9 Jordan Block Matrices

Johns Hopkins University Department of Biostatistics Math Review for Introductory Courses

ELEMENTS OF NUMBER THEORY. In the following we will use mainly integers and positive integers. - the set of integers - the set of positive integers

MAX-MIN AND MIN-MAX VALUES OF VARIOUS MEASURES OF FUZZY DIVERGENCE

Transcription:

Comutatos wth large umbers Wehu Hog, Det. of Math, Clayto State Uversty, 2 Clayto State lvd, Morrow, G 326, Mgshe Wu, Det. of Mathematcs, Statstcs, ad Comuter Scece, Uversty of Wscos-Stout, Meomoe, WI 5475 bstract. Sce ay ersoal comuter has a lmted rage of teger values, therefore, t wll result a teger overflow whe a rogram tres to comute a value larger tha mache s maxmum value. We wll dscuss a worable algorthm that wll be able to deal wth ay large umbers wthout gettg a teger overflow. Keywords: Iteger overflow.. Itroducto y rogrammer ows that whe a varable s declared to be of teger tye, t has a maxmum value. It wll result a teger overflow whe a rogram s to comute a teger value larger tha your mache s maxmum value. Some mache gves you a error message whe overflow occurs, but others do t. Therefore, t s ecessary to revet t from occurrg. It would be ce to fd a algorthm that wll be able to deal wth ay large umber wthout gettg a teger overflow o ay mache. I the followg, we wll rovde a algorthm that wll fulfll such goal. 2. lgorthm Our aroach s smle ad easy for mlemetato. The dea s to tae ay teger ut as a strg, whch ca be as log as you wsh. For stace, f you have to calculate 2345678923456789986272727282222234 + 234598726262626262626262626262688889982 you ca vew both umbers as strg, the maulate them ether dgt by dgt or bloc by bloc. The followg algorthm dd (, ) s a algorthm of dgt by dgt. lgorthm dd (, ) : = ( f = where the C C 2 2 Italze Carry = ; For ( = ; < ; ) Outut : C = C, where s the ut dgt, s the ut dgt, +, ot of the same legth, fll f + + Carry > 9, f Carry >, the C = + + Carry, Carry = + C C C C s the teth dgt, ad so o s the teth dgt, ad so o = + + Carry, Carry = = Carry C t out by zeros to the left) + = "";

Cosderg the effcecy for large umbers, we mght dvde each umber to blocs of legth that ca be determed by the mache s maxmum teger, ad the use regular oerato of addto to carry out the sum of each bloc, ad the cocateate the sums of blocs to get the sum. The followg algorthm demostrates the dea of bloc by bloc. lgorthm dd2 (, ) ssume that s a ostve whole umber; = = where ( + + ( ) ( ) ) ad, + + ( 2 2 ( 2) ( 2) + ; + ; ) are whole umbers of or fewer dgts; Each sum + + ( =,, 2,,m(, s less tha the mache'max; s () Use regular addto to fd each sum S If S. legth = +, the Carry =, otherwse, Carry = ; Set S = ( f > ) or ( f = + ( =,,,m(, ; ) for = m(, ) + to max(, ); (2) If ( Carry = ) the ( S C = ""& S ); = S ; ach ( S. legth) s to the left of S ; (3) For ( =, 2,, max(, S = S f ( S Carry = ; S C = S + Carry;. legth < + ) the f ( < max(, the ach ( S C = S & C; Carry = ; ach ( S & C; = S ;. legth) s to the left of S. legth) s to the left of ;) S ; (4) If ( Carry = ) the C = ""& C; (5) Outut C as the sum +.

Lewse, we ca do subtracto a smlar way. The followg s a algorthm for subtracto dgt by dgt. We ca also do subtracto bloc by bloc as show lgorthm Subtract2 (, ). lgorthm Subtract2 (, ) : ssume that s a ostve whole umber; = = where ( =,, 2, 3,, ) ad, whole umbers of ad ad lgorthm Subtract (, ) : = = ( f C + + () f ( = ) the ot of the C Outut : f 2 2 sg = " ";, Italze orrow = ; For ( = ; < ; ) where s the ut dgt, are less tha the mache'max; s outut D = ; Ext; ( ) ( ) + + 2 dgts ( ach s f are whole umbers of f ( > ) the sg = ' + ;' sg = ' ;' swa ad ;, where s the ut dgt, If the sg = " + " f orrow <, ( sg == " ") the C C s the teth dgt, ad so o s the teth dgt, ad so o the same legth, fll t out by zeros to the left = orrow +, orrow = = orrow, orrow = C C C 2 ( 2) ( 2) C ( =,, 2, 3,, ) are ecessary); or fewer dgts; ) + ; + ;

(2) ssume > ; Italze orrow = ; Use regular subtracto to fd each dfferece : For ( = to max(, f ( m(, the f ( f ( (3) Let m = max(, ); (4) For( = to m ), ach ( D. legth) s (5) D = ""; for( = to m) D = D (6) f ( sg = ' )' the D = sg & D; (7) Outut orrow > ) the ( D ( D orrow > ) the ( D ( D = + = + orrow; orrow = ;) & D; D as the dfferece. = orrow; orrow = ;) orrow; orrow = ;) = orrow; orrow = ;) to the left of D ; For multlcato, we ca do ether dgt wse or bloc wse. The followg s a algorthm for multlcato bloc wse. lgorthm Multly (, ) : ssume that = = where, () Use regular multlcato to (2) ttchg ( + ) (3) Use the dd(, ) or dd 2(, ) to sum u all roducts; (4) Outut + + Each roduct ( ) ( ) ( ; s less the sum as the + + 2 2 ) are ( 2) ( 2) tha the mache'max; s zeros to the ed whole umbers of fd each roduct ; roduct of ad. of the roduct ; + ; + ; or fewer dgts, I order to chec dvsblty ad rmalty of tegers, we eed a algorthm of dvso. We ca roceed as follow: lgorthm Dvde (, ) : = where s the ut dgt, s the teth dgt, ad so o = m where m 2 m 2,, s the ut dgt, s the teth dgt, ad so o

(). If <, the Outut uotet =, ad remader =. (2). If =, the Outut uotet =, ad remader =. (3). If >, the Dvded =, dvsor =, K = the legth of ; Whle (Dvded > dvsor) D = the frst dgts of Dvded from the left. D2 = the substrg of Dvded startg from the (+) to the ed If (D >= dvsor) the Fd the greatest o-egatve teger such that (dvsor)<= D; cout = ; Udate Dvded = Subtract (D, Multly (, dvsor)) & D2; Else = ; cout D = the frst + dgts of Dvded D2 = the substrg of Dvded startg from the (+2) to the ed Fd the greatest o-egatve teger such that (dvsor) <= D; cout = ; Udate Dvded = Subtract (D, Multly (, dvsor)) & D2; If (Dvded = dvsor) the = ; Remader = ; cout Else Remader = Dvded; Outut : uotet = 2cout remader = Remader Oce we mlemet Dvde (, ), we ca defe modulo fucto Mod (, ), whch returs the remader whe s dvded by. Therefore, we ca chec dvsblty ad rmalty of tegers. 3. lcatos It s very commo that wheever we teach Number Theory, we eed dslay some large umbers such as Fboacc rmes ad Mersee umbers by usg comuter. If we are ot careful, we wll ru to a teger overflow. We ca use the algorthms develoed secto 2 above to elmate such errors. s a demostrato, we gve two examles the followg. The frst examle s a comarso of dslays of large umbers o comuters wth or wthout usg our algorthms.

Examle. It s well ow [ 2] that the 3 rd, 4 th,7 th, th, 3 th,7 th,23 rd, 29 th,43 rd,47 th,83 rd,3 st, 37 th, 359 th,43 st, 433 rd,449 th,59 th,569 th,57 st,297 st,4723 rd,5387 th,93 th,9677 th,443 st, 2556 st, 3757 th, 3757 th, 35999 th, ad 8839 th terms of Fboacc seuece are rmes. It s easy to dslay ay of the frst few small oes usg a comuter. However, t s mossble to dslay a large oe wthout gettg a teger overflow. I the table, t was show that the dslay va JavaScrt wthout usg our algorthm wll ether loss recsos or get a scetfc otato that ufortuately results a real umber stead of a teger. lve demostrato s avalable o our webste at htt://cms.clayto.edu/whog/tools/demo4gnumber.htm. The Dslay wthout usg our algorthm Table Fboacc Prmes. The frst 22 rmes the Fboacc Seuece The Dslay usg our algorthm 2 2 3 3 5 5 3 3 89 89 233 233 597 597 28657 28657 54229 54229 433494437 433494437 2972573 2972573 99948539475549 999485394755497.6634474977e+ 6634474975958457269 27.93472493282e+ 93472493278844942397 28 4.75424377346978e+7 475424377346982274736827667493829277476557936622687637693547624 4 5.298927695e+8 529892769562792395567877846797275929534566295628347699553 9 4424689676262369.38727727847825e+ 38727727847838274863862463922584535877836979983236252259 9 546259372568353 3.679992484544e+ 367999248454535543384883772828543235373849736747993257238 93 49593344285665949.5979992653477e+ 5979992653497325996436755342558643549429325696842974347 6 954834293254396957698762999 3.668447436894e+ 3668447436897864736364627563455869952298527242868477686 8 93568579433578795455228437777865869 9.6426892246e+ 964268922553823942883369248652649748776786822264789294378 8 384788649258984852543363764683874629 3573566499867297774396345444556992658284336794499747637 27675748334356358578349544485856263927386498933944692 9285676835268346883754423234279785257659247472936685765568649773 3524867828777656879686368266736535884926393775439256896322343 Ifty 7588287692449869883794932475663743499635834369899258 47723827475594236522942242943792882633885466569679755992743 52632522294562989922638267958923434738522823366284948672297 22772926469582342687226427455862972679295259595434968348595 855237486424384736229

Examle 2. It s well ow [ ] that the 257 th Mersee umber s a rme. To dslay the 257 th Mersee umber, we ca mae a smle loo as follows: Cout = ; X = ; Whle (Cout Not Eual to 257) X = Multly(X,"2"); Z = Subtract(X,""); Cout++; Outut Z; The 257 th Mersee umber s 23584784746323984749773758576539969332828789568582625927987. 4. Cocluso It s very commo that a error of teger overflow occurs scetfc comutato. To elmate such a error, t s a good dea to mlemet algorthms as we dd ths ote ether dgt by dgt or bloc by bloc so that rograms wll be able to hadle stuatos tellgetly for lager tegers. The algorthm gve ths ote ca be easly aled to may scetfc comutatos such as RS [3] ecryto ad searchg for large rmes. cowledgmets: We tha Dr. ust ad Dr. Mg-Ju La for ther may suggestos that mroved ths ote. Refereces [] Davd M. urto, 27, Elemetary Number Theory, the 6 th Ed., McGraw Hll. [2] Chrs K. Caldwell, Fboacc Prmes, htt://rmes.utm.edu [3]. ruce Scheer, 996, led Crytograhy, Protocols, lgorthms, ad Source Code C, the 2 d Ed., Wley.