aboutsummaryrefslogtreecommitdiff
path: root/challenge-158/paulo-custodio/python/ch-2.py
blob: 1f7220d82b6e09a0dbd78dfb3d760d483d43651d (plain)
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
#!/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))))