diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-28 19:23:13 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-28 19:23:13 +0100 |
| commit | 341287fb7780a3c5ee45ec0521ef99d11dbdbcce (patch) | |
| tree | b7793f10622a828e704e919487e77ffcce8a21a8 | |
| parent | 19698b60b5f8bbc16989a16c47fe41fa435cede3 (diff) | |
| download | perlweeklychallenge-club-341287fb7780a3c5ee45ec0521ef99d11dbdbcce.tar.gz perlweeklychallenge-club-341287fb7780a3c5ee45ec0521ef99d11dbdbcce.tar.bz2 perlweeklychallenge-club-341287fb7780a3c5ee45ec0521ef99d11dbdbcce.zip | |
Add Python solution to challenge 155
| -rw-r--r-- | challenge-065/paulo-custodio/python/ch-2.py | 2 | ||||
| -rw-r--r-- | challenge-154/paulo-custodio/python/ch-1.py | 2 | ||||
| -rw-r--r-- | challenge-154/paulo-custodio/python/ch-2.py | 2 | ||||
| -rw-r--r-- | challenge-155/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-155/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-155/paulo-custodio/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-155/paulo-custodio/python/ch-2.py | 57 |
7 files changed, 94 insertions, 5 deletions
diff --git a/challenge-065/paulo-custodio/python/ch-2.py b/challenge-065/paulo-custodio/python/ch-2.py index 702ed4d413..7d037bdc0d 100644 --- a/challenge-065/paulo-custodio/python/ch-2.py +++ b/challenge-065/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 065 # diff --git a/challenge-154/paulo-custodio/python/ch-1.py b/challenge-154/paulo-custodio/python/ch-1.py index 7deb2cd296..1b121593e6 100644 --- a/challenge-154/paulo-custodio/python/ch-1.py +++ b/challenge-154/paulo-custodio/python/ch-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 154 # diff --git a/challenge-154/paulo-custodio/python/ch-2.py b/challenge-154/paulo-custodio/python/ch-2.py index 91384eb9f6..0afb22504f 100644 --- a/challenge-154/paulo-custodio/python/ch-2.py +++ b/challenge-154/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 154 # diff --git a/challenge-155/paulo-custodio/perl/ch-1.pl b/challenge-155/paulo-custodio/perl/ch-1.pl index ff00af24a0..80eb7c1565 100644 --- a/challenge-155/paulo-custodio/perl/ch-1.pl +++ b/challenge-155/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 155 # -# TASK #1 › Fortunate Numbers +# TASK #1 > Fortunate Numbers # Submitted by: Mohammad S Anwar # Write a script to produce first 8 Fortunate Numbers (unique and sorted). # diff --git a/challenge-155/paulo-custodio/perl/ch-2.pl b/challenge-155/paulo-custodio/perl/ch-2.pl index 4ff1516026..fbaa92c2ee 100644 --- a/challenge-155/paulo-custodio/perl/ch-2.pl +++ b/challenge-155/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 155 # -# TASK #2 › Pisano Period +# TASK #2 > Pisano Period # Submitted by: Mohammad S Anwar # Write a script to find the period of the 3rd Pisano Period. # diff --git a/challenge-155/paulo-custodio/python/ch-1.py b/challenge-155/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..e1cdaebf76 --- /dev/null +++ b/challenge-155/paulo-custodio/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +# Challenge 155 +# +# TASK #1 > Fortunate Numbers +# Submitted by: Mohammad S Anwar +# Write a script to produce first 8 Fortunate Numbers (unique and sorted). +# +# According to Wikipedia +# +# A Fortunate number, named after Reo Fortune, is the smallest integer m > 1 +# such that, for a given positive integer n, pn# + m is a prime number, where +# the primorial pn# is the product of the first n prime numbers. +# +# Expected Output +# 3, 5, 7, 13, 17, 19, 23, 37 + +from math import prod +from sympy import isprime, nextprime + +fortunate = {} + +primes = [2] +while len(fortunate) < 8: + p = prod(primes) + m = 2 + while not isprime(p + m): + m += 1 + fortunate[m] = 1 + primes.append(nextprime(primes[-1])) + +print(", ".join(map(str, sorted(fortunate.keys())))) diff --git a/challenge-155/paulo-custodio/python/ch-2.py b/challenge-155/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..f5408b05db --- /dev/null +++ b/challenge-155/paulo-custodio/python/ch-2.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +# Challenge 155 +# +# TASK #2 > Pisano Period +# Submitted by: Mohammad S Anwar +# Write a script to find the period of the 3rd Pisano Period. +# +# In number theory, the nth Pisano period, written as p(n), is the period with +# which the sequence of Fibonacci numbers taken modulo n repeats. +# +# The Fibonacci numbers are the numbers in the integer sequence: +# +# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, ... +# For any integer n, the sequence of Fibonacci numbers F(i) taken modulo n is +# periodic. The Pisano period, denoted p(n), is the value of the period of this +# sequence. For example, the sequence of Fibonacci numbers modulo 3 begins: +# +# 0, 1, 1, 2, 0, 2, 2, 1, +# 0, 1, 1, 2, 0, 2, 2, 1, +# 0, 1, 1, 2, 0, 2, 2, 1, ... +# This sequence has period 8, so p(3) = 8. + +from math import isqrt + +order = 3 + +def fibonacci_series(n): + fibs = [0, 1] + for i in range(2, n): + fibs.append(fibs[-1] + fibs[-2]) + return fibs + +def is_period(period, n): + base = n[0:period] + ord1 = n[period:2*period] + ord2 = n[2*period:3*period] + + if base != ord1: + return 0 + if base != ord2: + return 0 + return 1 + +def find_period(n): + period = 1 + while True: + if 3 * period > len(n): + return 0 + if is_period(period, n): + return period + period += 1 + return 0 + +fibs = fibonacci_series(100) +fibs_mod = [fib % order for fib in fibs] +print(find_period(fibs_mod)) |
