2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-26 06:48:04 +02:00
LaTeX-examples/source-code/Pseudocode/Euklidischer-Algorithmus/basiswechsel.py

38 lines
716 B
Python
Raw Normal View History

2013-06-11 21:59:02 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2015-11-20 22:36:38 +01:00
2013-06-11 21:59:02 +02:00
def euklid(b, Z):
2015-11-20 22:36:38 +01:00
"""
Euclids algorithm to change the basis.
Returns
-------
dict
A dictionary mapping the i-th position of the new number to its value,
where higher numbers are more significant.
Examples
--------
>>> euklid(3, 5)
{1: 1, 0: 2}
"""
2013-06-11 21:59:02 +02:00
p = 0
while b**p <= Z:
p = p+1
i = p - 1
y = {}
while Z != 0 and i > -5:
2015-11-20 22:36:38 +01:00
y[i] = Z // b**i
2013-06-11 21:59:02 +02:00
R = Z % b**i
Z = R
2015-11-20 22:36:38 +01:00
i = i - 1
2013-06-11 21:59:02 +02:00
return y
if __name__ == "__main__":
r = euklid(16, 15741.233)
print("Result:")
2015-11-20 22:36:38 +01:00
for key in sorted(r.iterkeys(), reverse=True):
2013-06-11 21:59:02 +02:00
print "%s: %s" % (key, r[key])