티스토리 뷰
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.

In general, a positive natural number that can be multiplied by by moving the rightmost digit to the front of the number is called an -parasitic number. Here is itself a single-digit positive natural number. For example
so 128205 is a 4-parasitic number. Natural numbers with leading zeros are not allowed. So even though
the number 025641 is not 4-parasitic.
The corkscrew method is a technique that can be used to derive an -parasitic number the ends with a given digit , which should be chosen such that . The method generates the -parasitic number one digit at a time, starting with rightmost digit. In other words, the -parasitic number is constructed right to left. This is illustrated below for and .

The method thus progresses by gradually taking one more digit from the end of the result of the multiplication, appending the digit at the end of this sequence of digits, and multiplying the resulting number by . The first time we compute , take the last digit of the result, append the digit and multiply that number by . Then we take the last two digits from the result of the multiplication, append the digit and multiply this number by . We keep on repeating this procedure until the multiplication results in a number that is the same as the number that was multiplied by , 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 -parasitic, the function must return the value . Otherwise, the function must return the value 0.
Write a function corkscrew that takes two natural numbers and . The function must return the -parasitic number that ends with the digit that results from applying the corkscrew method. The function may assume that and are both single-digit natural numbers, with .
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.


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 |
- Total
- Today
- Yesterday
- gitignore
- 장고
- wsgi
- 문서 비교
- 면접답변
- pycrypto
- virtualenv
- PuTTYGen
- GIT
- 모바일 테마 적용
- 파이썬 독학
- 면접정답
- 플러스친구 자동응답
- 모바일 스킨 적용
- Python
- admin.py
- 파이썬 입문
- chatbot
- 장고 플러스친구 자동응답
- 엑셀 비교
- gitlab
- 파이썬 프로그래밍
- 이미지 비교
- 파이썬 강좌
- django chatbot
- 장고 카톡 자동응답
- 파이썬
- Tistory
- django
- 문과 코딩
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |