티스토리 뷰

python lecture/algorism

[edu] Corkscrew

burningrizen 2018. 10. 4. 13:36

What's so special about the number 105263157894736842? This number doubles if you move the rightmost digit to the front of the number. In addition, this is the smallest natural number that has this property.

105263157894736842

In general, a positive natural number that can be multiplied by n by moving the rightmost digit to the front of the number is called an n-parasitic number. Here n is itself a single-digit positive natural number. For example

4×128205=512820

so 128205 is a 4-parasitic number. Natural numbers with leading zeros are not allowed. So even though

4×025641=102564

the number 025641 is not 4-parasitic.

The corkscrew method is a technique that can be used to derive an n-parasitic number the ends with a given digit k, which should be chosen such that kn. The method generates the n-parasitic number one digit at a time, starting with rightmost digit. In other words, the n-parasitic number is constructed right to left. This is illustrated below for n=4 and k=7.

corkscrew method

The method thus progresses by gradually taking one more digit from the end of the result of the multiplication, appending the digit k at the end of this sequence of digits, and multiplying the resulting number by n. The first time we compute n×k, take the last digit of the result, append the digit k and multiply that number by n. Then we take the last two digits from the result of the multiplication, append the digit k and multiply this number by n. We keep on repeating this procedure until the multiplication results in a number that is the same as the number that was multiplied by n, but with the rightmost digit moved to the front.

Assignment

The operation where the leftmost digit is moved to the end of a natural number is called a left rotation. The operation where the rightmost digit is moved to the front of a natural number is called a right rotation. In both cases, any leading zeros that might pop up are removed. Your task:

  • Write a function rotateLeft that takes a natural number. The function must return the natural number that results after applying a left rotation on the given natural number.

  • Write a function rotateRight that takes a natural number. The function must return the natural number that results after applying a right rotation on the given natural number.

  • Write a function parasitic that takes a natural number. In case the given natural number is n-parasitic, the function must return the value n. Otherwise, the function must return the value 0.

  • Write a function corkscrew that takes two natural numbers n and k. The function must return the n-parasitic number that ends with the digit k that results from applying the corkscrew method. The function may assume that n and k are both single-digit natural numbers, with kn.

Example

>>> rotateLeft(717948)
179487
>>> rotateLeft(142857)
428571
>>> rotateLeft(105263157894736842)
52631578947368421

>>> rotateRight(179487)
717948
>>> rotateRight(428571)
142857
>>> rotateRight(52631578947368421)
15263157894736842

>>> parasitic(179487)
4
>>> parasitic(142857)
5
>>> parasitic(105263157894736842)
2
>>> parasitic(1234)
0

>>> corkscrew(4, 7)
179487
>>> corkscrew(5, 7)
142857
>>> corkscrew(2, 2)
105263157894736842

For debugging purposes, the figures below apply the corkscrew method for the two other examples in the above sample session.

corkscrew method corkscrew method

Resources

  • Bernstein L (1968). Multiplicative twins and primitive roots. Mathemetische Zeitschrift 105, 49-58. 

  • Pickover CA (2000). Wonders of Numbers. Oxford University Press










v 0.1

def remove_zero(str_num):
if str_num[0] == "0":
str_num = str_num[1:]
return int(str_num)


def rotateLeft(num):
str_num = str(num)
str_rotate = str_num[1:] + str_num[0]
return remove_zero(str_rotate)


def rotateRight(num):
str_num = str(num)
str_rotate = str_num[-1:] + str_num[:-1]
return remove_zero(str_rotate)


def corkscrew(n, k):
screw_k = k
str_k = str(k)
cnt_digit = 0
while True:
num_multiple = n * screw_k
str_multiple = str(num_multiple)
cnt_digit += 1
if str_multiple[0] == str_k:
break
screw_k = int((str_multiple[1:] if len(str_multiple) > cnt_digit else str_multiple) + str_k)
return screw_k


def parasitic(num):
tail = int(str(num)[-1:])
for i in range(1, 10):
if num == corkscrew(i, tail):
return i
return 0









'python lecture > algorism' 카테고리의 다른 글

[edu] Word sums  (0) 2018.10.08
[edu] Penney Ante  (0) 2018.10.06
[eud] Cowsay  (0) 2018.10.02
[edu] Wepe speapeak p  (0) 2018.10.02
[edu] Pythagorean triples  (0) 2018.10.02
댓글