Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
5
def euklid(b, Z):
6
"""
7
Euclids algorithm to change the basis.
8
9
Returns
10
-------
11
dict
12
A dictionary mapping the i-th position of the new number to its value,
13
where higher numbers are more significant.
14
15
Examples
16
--------
17
>>> euklid(3, 5)
18
{1: 1, 0: 2}
19
"""
20
p = 0
21
while b**p <= Z:
22
p = p+1
23
i = p - 1
24
25
y = {}
26
while Z != 0 and i > -5:
27
y[i] = Z // b**i
28
R = Z % b**i
29
Z = R
30
i = i - 1
31
return y
32
33
if __name__ == "__main__":
34
r = euklid(16, 15741.233)
35
print("Result:")
36
for key in sorted(r.iterkeys(), reverse=True):
37
print "%s: %s" % (key, r[key])
38
39