diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-14 13:42:10 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-14 13:42:10 +0100 |
| commit | 41b7f521b911e48b80286df701186f18d2bfdff3 (patch) | |
| tree | e883564eebb37d69c67b4cc86f9de3e090f3482f | |
| parent | 268681aa37ab8f048e6f595608578f1154fe2416 (diff) | |
| download | project_euler-41b7f521b911e48b80286df701186f18d2bfdff3.tar.gz project_euler-41b7f521b911e48b80286df701186f18d2bfdff3.tar.bz2 project_euler-41b7f521b911e48b80286df701186f18d2bfdff3.zip | |
problem 49 in python
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | python/049-prime_permutations.py | 32 |
2 files changed, 34 insertions, 0 deletions
@@ -1,5 +1,7 @@ # Project Euler solutions + + My attempt at solving some of the [Project Euler](https://projecteuler.net/) problems. I try to solve each problem in multiple languages: diff --git a/python/049-prime_permutations.py b/python/049-prime_permutations.py new file mode 100644 index 0000000..95a0ab6 --- /dev/null +++ b/python/049-prime_permutations.py @@ -0,0 +1,32 @@ +### +# Prime permutations +# Problem 49 +# +# The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. +# There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. +# What 12-digit number do you form by concatenating the three terms in this sequence? +### + + +import math + + +def is_prime(n): + if n % 2 == 0 or n % 3 == 0 or n % 5 == 0: + return False + for d in range(6, math.floor(math.sqrt(n)) + 1, 6): + if n % (d - 1) == 0 or n % (d + 1) == 0: + return False + return True + + +for n1 in range(1001, 10000, 2): + for n2 in range(n1 + 2, 10000, 2): + n3 = n2 + (n2 - n1) + s = sorted(str(n1)) + if s != sorted(str(n2)) or s != sorted(str(n3)): + continue + if is_prime(n1) and is_prime(n2) and is_prime(n3): + print(n1, n2, n3) + + |
