diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-28 21:03:54 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-28 21:03:54 +0100 |
| commit | 45ae6460d987618b7ca6016b89f7722c848893fa (patch) | |
| tree | 845871ca1bc5bcedd4e57574aac893a52fd5f164 /challenge-158/paulo-custodio/python/ch-2.py | |
| parent | bcbb8e543ac9ad74cf70fb749a5f7f917e78abb2 (diff) | |
| download | perlweeklychallenge-club-45ae6460d987618b7ca6016b89f7722c848893fa.tar.gz perlweeklychallenge-club-45ae6460d987618b7ca6016b89f7722c848893fa.tar.bz2 perlweeklychallenge-club-45ae6460d987618b7ca6016b89f7722c848893fa.zip | |
Add Python solution to challenge 158
Diffstat (limited to 'challenge-158/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-158/paulo-custodio/python/ch-2.py | 31 |
1 files changed, 31 insertions, 0 deletions
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)))) |
