-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths10_automorphic_numbers.py
More file actions
92 lines (69 loc) · 1.75 KB
/
s10_automorphic_numbers.py
File metadata and controls
92 lines (69 loc) · 1.75 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""S10-Automorphic Numbers.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1vdVfQwsz2pPtvazzKsqCL4wMcOfK_fJ5
**Our Naive Try**
"""
import math
order = int(input())
o = 4
a = 5
i5 = 5
i6 = 6
while True:
for d in range(1,11):
n5 = math.ceil(math.log10(i5))
a = d*(10**n5)+i5
nn5 = math.ceil(math.log10(a))
a2 = a**2
if (a2-a)%(10**nn5)==0:
o+=1
i5=a
n6 = math.ceil(math.log10(i6))
b = d*(10**n6)+i6
b2 = b**2
nn6 = math.ceil(math.log10(b))
if (b2-b)%(10**nn6)==0:
o+=1
i6=b
#print(n5,n6,a,b,i5,i6)
if o>=order:
break
if o>order:
print(min(i5,i6))
else:
print(max(i5,i6))
"""**Based on a math formula, much much faster**"""
d = 20
# set up variables
total = 3
n_five = 5
n_six = 6
# print a title and the first 3 digits
print( "Automorphic Numbers up to " + str(d) + " Digits Long:")
print( " 1")
print(" 5")
print(" 6")
# find new n_five and n_six automorphic numbers based on the equations
# n' = -2n^3 + 3n^2 (mod 10^(k + 1)) and n_five + n_six = 10^k + 1,
# for n as a known automorphic number with k digits
for k in range(1, d):
n_five = (-2 * n_five ** 3 + 3 * n_five ** 2) % (10 ** (k + 1))
n_six = 10 ** (k + 1) + 1 - n_five
if n_six < n_five:
if n_six > (10 ** k):
print (" " + str(n_six))
total += 1
if n_five > (10 ** k):
print (" " + str(n_five))
total += 1
else:
if n_five > (10 ** k):
print (" " + str(n_five))
total += 1
if n_six > (10 ** k):
print (" " + str(n_six))
total += 1
# print the total
print ("Total: " + str(total))