forked from heineman/LearningAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmult.py
More file actions
38 lines (32 loc) · 892 Bytes
/
mult.py
File metadata and controls
38 lines (32 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Multiply two n-digit numbers (where n is quite large) of the following form
1234567891234567 x 9876543219876543
"""
import random
def create_pair(n):
"""Create a pair of n-digit integers, from 1-up and from 9-down."""
one = 0
two = 0
up = 1
down = 9
num_digits = 0
while num_digits < n:
one = 10*one + up
two = 10*two + down
up += 1
if up == 10: up = 1
down -= 1
if down == 0: down = 9
num_digits += 1
return [one, two]
def create_random_pair(n):
"""Create a pair of n-digit integers, containing digits from 1-9 only."""
one = 0
two = 0
for _ in range(n):
one = 10*one + random.randint(1,9)
two = 10*two + random.randint(1,9)
return [one, two]
def mult_pair(pair):
"""Return the product of two, potentially large, numbers."""
return pair[0]*pair[1]