From 45ae6460d987618b7ca6016b89f7722c848893fa Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 21:03:54 +0100 Subject: Add Python solution to challenge 158 --- challenge-158/paulo-custodio/perl/ch-1.pl | 2 +- challenge-158/paulo-custodio/perl/ch-2.pl | 2 +- challenge-158/paulo-custodio/python/ch-1.py | 27 +++++++++++++++++++++++++ challenge-158/paulo-custodio/python/ch-2.py | 31 +++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 challenge-158/paulo-custodio/python/ch-1.py create mode 100644 challenge-158/paulo-custodio/python/ch-2.py diff --git a/challenge-158/paulo-custodio/perl/ch-1.pl b/challenge-158/paulo-custodio/perl/ch-1.pl index 1c23f8c0ff..c71f3fa9d0 100644 --- a/challenge-158/paulo-custodio/perl/ch-1.pl +++ b/challenge-158/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 158 # -# TASK #1 › Additive Primes +# TASK #1 > Additive Primes # Submitted by: Mohammad S Anwar # Write a script to find out all Additive Primes <= 100. # diff --git a/challenge-158/paulo-custodio/perl/ch-2.pl b/challenge-158/paulo-custodio/perl/ch-2.pl index 8f28f63531..9d5cb05d83 100644 --- a/challenge-158/paulo-custodio/perl/ch-2.pl +++ b/challenge-158/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 158 # -# TASK #2 › First Series Cuban Primes +# TASK #2 > First Series Cuban Primes # Submitted by: Mohammad S Anwar # Write a script to compute first series Cuban Primes <= 1000. Please refer # wikipedia page for more informations. diff --git a/challenge-158/paulo-custodio/python/ch-1.py b/challenge-158/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..ad3b7825eb --- /dev/null +++ b/challenge-158/paulo-custodio/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +# Challenge 158 +# +# TASK #1 > Additive Primes +# Submitted by: Mohammad S Anwar +# Write a script to find out all Additive Primes <= 100. +# +# Additive primes are prime numbers for which the sum of their decimal digits +# are also primes. +# +# +# Output +# 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 + +from sympy import isprime, nextprime + +def additive_primes(limit): + out = [] + prime = 2 + while prime < limit: + if isprime(sum(int(digit) for digit in str(prime))): + out.append(prime) + prime = nextprime(prime) + return out + +print(", ".join(map(str, additive_primes(100)))) diff --git a/challenge-158/paulo-custodio/python/ch-2.py b/challenge-158/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1f7220d82b --- /dev/null +++ b/challenge-158/paulo-custodio/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# Challenge 158 +# +# TASK #2 > First Series Cuban Primes +# Submitted by: Mohammad S Anwar +# Write a script to compute first series Cuban Primes <= 1000. Please refer +# wikipedia page for more informations. +# +# Output +# 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919. +# +# p=(x^3-y^3)/(x-y), x=y+1, y>0 +# <=> p=3y^2+3y+1, y>0 + +from sympy import isprime + +def cuban_primes(limit): + out = [] + y = 1 + p = 0 + while True: + p = 3 * y * y + 3 * y + 1 + if isprime(p): + out.append(p) + if p >= limit: + break + y += 1 + return out + +print(", ".join(map(str, cuban_primes(1000)))) -- cgit