From 6fc1c623577ffe0e1a0ef89d8c2a6c7d87f0a6ca Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 3 Oct 2024 13:41:49 +0100 Subject: Add Python solution to challenge 170-task 1 --- challenge-170/paulo-custodio/perl/ch-1.pl | 8 ++--- challenge-170/paulo-custodio/perl/ch-2.pl | 2 +- challenge-170/paulo-custodio/python/ch-1.py | 52 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 challenge-170/paulo-custodio/python/ch-1.py diff --git a/challenge-170/paulo-custodio/perl/ch-1.pl b/challenge-170/paulo-custodio/perl/ch-1.pl index 4d36966785..3d07569f3b 100644 --- a/challenge-170/paulo-custodio/perl/ch-1.pl +++ b/challenge-170/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 170 # @@ -14,9 +14,9 @@ # # P(0) = 1 (1) # P(1) = 2 (1x2) -# P(2) = 6 (1x2×3) -# P(3) = 30 (1x2×3×5) -# P(4) = 210 (1x2×3×5×7) +# P(2) = 6 (1x2x3) +# P(3) = 30 (1x2x3x5) +# P(4) = 210 (1x2x3x5x7) use Modern::Perl; diff --git a/challenge-170/paulo-custodio/perl/ch-2.pl b/challenge-170/paulo-custodio/perl/ch-2.pl index dfff30bef8..aa101be3a9 100644 --- a/challenge-170/paulo-custodio/perl/ch-2.pl +++ b/challenge-170/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 170 # diff --git a/challenge-170/paulo-custodio/python/ch-1.py b/challenge-170/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..1b2a973b87 --- /dev/null +++ b/challenge-170/paulo-custodio/python/ch-1.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# Challenge 170 +# +# Task 1: Primorial Numbers +# Submitted by: Mohammad S Anwar +# +# Write a script to generate first 10 Primorial Numbers. +# +# Primorial numbers are those formed by multiplying successive prime numbers. +# +# +# For example, +# +# P(0) = 1 (1) +# P(1) = 2 (1x2) +# P(2) = 6 (1x2x3) +# P(3) = 30 (1x2x3x5) +# P(4) = 210 (1x2x3x5x7) + +import sys + +def is_prime(n): + if n == 2 or n == 3: + return True + if n <= 1 or n % 2 == 0 or n % 3 == 0: + return False + i = 5 + while i * i <= n: + if n % i == 0 or n % (i + 2) == 0: + return False + i += 6 + return True + +def next_prime(p): + p += 1 + while not is_prime(p): + p += 1 + return p + +def primorial_numbers(n): + primorial = [1] + p = 1 + while len(primorial) < n: + p = next_prime(p) + primorial.append(primorial[-1] * p) + return primorial + +if len(sys.argv) != 2: + raise ValueError("usage: ch-1.py n") +n = int(sys.argv[1]) +print(", ".join(map(str, primorial_numbers(n)))) -- cgit