aboutsummaryrefslogtreecommitdiff
path: root/challenge-158/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-04-03 21:40:08 +1000
committerSimon Green <mail@simon.green>2022-04-03 21:40:08 +1000
commit993e01103a73e4965f968a9d6bc08426ab51b6f9 (patch)
treea01b1898297b5c24ed9bc7360c2570f7a79ec4e1 /challenge-158/sgreen/python/ch-2.py
parentcac254d63bca6e312efe7f414b164e6e0510d1bb (diff)
downloadperlweeklychallenge-club-993e01103a73e4965f968a9d6bc08426ab51b6f9.tar.gz
perlweeklychallenge-club-993e01103a73e4965f968a9d6bc08426ab51b6f9.tar.bz2
perlweeklychallenge-club-993e01103a73e4965f968a9d6bc08426ab51b6f9.zip
sgreen solutions to challenge 158
Diffstat (limited to 'challenge-158/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-158/sgreen/python/ch-2.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-158/sgreen/python/ch-2.py b/challenge-158/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..9d741e6a15
--- /dev/null
+++ b/challenge-158/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+import math
+
+
+def is_prime(number):
+ # Return true or false if the number is a prime
+ if number < 2:
+ return False
+
+ for i in range(2, int(math.sqrt(number)) + 1):
+ if number % i == 0:
+ return False
+
+ # It's a prime
+ return True
+
+
+def main():
+ cuban_primes = []
+ x = 1
+
+ while x:
+ # Calculate 3x² + 3x + 1
+ p = 3 * x * x + 3 * x + 1
+
+ if p > 1000:
+ # We only care about values <= 1000
+ break
+
+ if is_prime(p):
+ # Add to the cuban_primes list
+ cuban_primes.append(p)
+
+ x += 1
+
+ print(*cuban_primes, sep=', ')
+
+
+if __name__ == '__main__':
+ main()