Skip to content

Commit a797ab8

Browse files
author
Albert Hu
authored
Merge pull request #15 from alberthu16/day21
Day 21: GCD with floats on codefights
2 parents 9c26c30 + 88ea6b1 commit a797ab8

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

day21/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Question of the day: https://codefights.com/challenge/hT9JyzWh77Y6NR2Qd
2+
3+
Stephan is a little boy who loves math. It came as a revelation to him that
4+
one can define the greatest common divisor for rational numbers, not just
5+
for integers. He's decided to dig into the subject and analyze the properties
6+
of this new-to-him GCD.
7+
8+
Stephan needs lots of GCDs to gather all possible information about them. But
9+
Stephan is still a little boy and he can't calculate the GCD of two numbers as
10+
quickly as he'd like. So he asks for your help: Given float numbers x and y,
11+
calculate their greatest common divisor.
12+
13+
It is guaranteed that both x and y will have at most 5 digits after the decimal
14+
point.
15+
16+
Example:
17+
18+
For `x = 2.4` and `y = 4.8`, the output should be
19+
`helpingStephan(x, y) = 2.4`.
20+
21+
## Ideas
22+
23+
How do I even find GCD without decimals? uhhhhh....
24+
25+
Prime factorization? That seems complicated but doable for small numbers.
26+
I can get the prime factorization of each number by repeatedly dividing
27+
primes in increasing magnitude up to the value of the number, and keeping
28+
the number of times each prime divides into the number in a hash. Then get
29+
the primes that each number has in common and get the `min` between them.
30+
31+
Let me just google this..
32+
33+
Aha! [Euclid's Algorithm](https://en.wikipedia.org/wiki/Greatest_common_divisor#Using_Euclid.27s_algorithm)
34+
I totally learned this in Algorithms class in college but forgot it existed.
35+
36+
Not sure what the runtime is for this algorithm, but this stack overflow post
37+
kinda explains it: http://stackoverflow.com/questions/3980416/time-complexity-of-euclids-algorithm
38+
39+
So I could just change the floats that I'm given into ints by multiplying by
40+
a factor of 10, apply Euclid's Algorithm in `O(n)` runtime where `n` is
41+
proportional to the value of the input numbers, and then divide by the earlier
42+
factor of 10 I used to multiply.
43+
44+
## Code
45+
46+
[Python](helpingStephan.py)
47+
48+
## Follow up
49+
50+
Floats.. https://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues

day21/helpingStephan.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def helpingStephan(x, y):
2+
digitsAfterDecimal = 5
3+
4+
x *= 10**digitsAfterDecimal
5+
y *= 10**digitsAfterDecimal
6+
7+
a = max(x, y)
8+
b = min(x, y)
9+
10+
r = -1
11+
while r != 0:
12+
r = round(a % b, 6)
13+
a = b
14+
b = r
15+
16+
return a / 10**digitsAfterDecimal
17+
18+
def main():
19+
assert helpingStephan(1, 1) == 1
20+
assert helpingStephan(2.4, 4.8) == 2.4
21+
assert helpingStephan(36.21, 4.19) == 0.01
22+
assert helpingStephan(4, 6) == 2
23+
assert helpingStephan(2.43, 4.899) == 0.003
24+
assert helpingStephan(47.804, 11.8683) == 0.0001
25+
assert helpingStephan(17.27568, 49.90752) == 1.91952
26+
assert helpingStephan(22.14728, 11.07364) == 11.07364
27+
28+
if __name__ == "__main__":
29+
main()

0 commit comments

Comments
 (0)