-
Notifications
You must be signed in to change notification settings - Fork 0
/
35.py
96 lines (86 loc) · 2.42 KB
/
35.py
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
93
94
95
96
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 19:56:57 2020
@author: santi
"""
def es_primo(n):
"""
Recibe un numero n y dice si es primo.
"""
for i in range(2,int(n**0.5)+1):
if n % i == 0:
return False
return True
def lista_primos(n):
lista = []
for i in range(2,n+1):
if es_primo(i):
lista.append(i)
return lista
def rotaciones(x):
lista = []
if len(x) <= 1:
return x
else:
for i in range(len(x)):
lista.append(x[i:] + x[:i])
return(lista)
def es_circular(n):
n = str(n)
for i in rotaciones(n):
i = int(i)
if not es_primo(i):
return False
return True
def es_truncable(n):
n = str(n)
for i in range(1,len(n)):
a = not es_primo(int(n[i:]))
b = not es_primo(int(n[:i]))
if a or b:
return False
from time import time
start = time()
lista_primos = lista_primos(100)
### 6 DIGITOS
for a in [1,3,7,9]:
for b in [1,3,7,9]:
for c in [1,3,7,9]:
for d in [1,3,7,9]:
for e in [1,3,7,9]:
for f in [1,3,7,9]:
i = f + 10*e + 100*d + 1000*c + 10000*b + 100000*a
if es_primo(i):
lista_primos.append(i)
### 5 DIGITOS
for a in [1,3,7,9]:
for b in [1,3,7,9]:
for c in [1,3,7,9]:
for d in [1,3,7,9]:
for e in [1,3,7,9]:
i = e + 10*d + 100*c + 1000*b + 10000*a
if es_primo(i):
lista_primos.append(i)
### 4 DIGITOS
for a in [1,3,7,9]:
for b in [1,3,7,9]:
for c in [1,3,7,9]:
for d in [1,3,7,9]:
i = d + 10*c + 100*b + 1000*a
if es_primo(i):
lista_primos.append(i)
### 3 DIGITOS
for a in [1,3,7,9]:
for b in [1,3,7,9]:
for c in [1,3,7,9]:
i = c + 10*b + 100*a
if es_primo(i):
lista_primos.append(i)
lista = []
for i in lista_primos:
# if es_circular(i):
# lista.append(i)
if es_truncable(i):
lista.append(i)
print(sum(lista))
print(time()-start)