diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-15 17:56:38 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-15 17:56:38 +0000 |
| commit | 7efe6d4ec97f274f4422d5ccaacdca6b9feaffb4 (patch) | |
| tree | 1af6ab3e27df8aeb79c7f8cb92314271421a46cc | |
| parent | 71081ca1fbcd2a5c3c1330d732f9dec368010a41 (diff) | |
| download | perlweeklychallenge-club-7efe6d4ec97f274f4422d5ccaacdca6b9feaffb4.tar.gz perlweeklychallenge-club-7efe6d4ec97f274f4422d5ccaacdca6b9feaffb4.tar.bz2 perlweeklychallenge-club-7efe6d4ec97f274f4422d5ccaacdca6b9feaffb4.zip | |
Add Python solution to challenge 8
| -rw-r--r-- | challenge-008/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/python/ch-1.py | 51 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/python/ch-2.py | 21 | ||||
| -rw-r--r-- | challenge-008/paulo-custodio/test.pl | 4 |
6 files changed, 76 insertions, 6 deletions
diff --git a/challenge-008/paulo-custodio/Makefile b/challenge-008/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-008/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-008/paulo-custodio/perl/ch-1.pl b/challenge-008/paulo-custodio/perl/ch-1.pl index 41a8010327..a0c9a72945 100644 --- a/challenge-008/paulo-custodio/perl/ch-1.pl +++ b/challenge-008/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 008 # diff --git a/challenge-008/paulo-custodio/perl/ch-2.pl b/challenge-008/paulo-custodio/perl/ch-2.pl index 09e628d446..8edbc236fa 100644 --- a/challenge-008/paulo-custodio/perl/ch-2.pl +++ b/challenge-008/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 008 # diff --git a/challenge-008/paulo-custodio/python/ch-1.py b/challenge-008/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..9e11164357 --- /dev/null +++ b/challenge-008/paulo-custodio/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# Challenge 008 +# +# Challenge #1 +# Write a script that computes the first five perfect numbers. A perfect number +# is an integer that is the sum of its positive proper divisors (all divisors +# except itself). Please check Wiki for more information. This challenge was +# proposed by Laurent Rosenfeld. + +import sys + +def is_prime(n): + if n <= 1: + return 0 + elif n <= 3: + return 1 + elif n % 2 == 0 or n % 3 == 0: + return 0 + else: + for i in range(5, n+1, 6): + if i*i>n: + break + if n % i == 0 or n % (i+2) == 0: + return 0 + return 1 + +def next_prime(n): + if n<=1: + return 2 + n += 1 + while not is_prime(n): + n += 1 + return n + +# Euclid proved that 2^(p-1)*(2^p - 1) is an even perfect number +# whenever 2p - 1 is prime +def perfect_iter(): + p = 1 + while True: + p = next_prime(p) + f = (2**p)-1 + if is_prime(f): + yield (2**(p-1))*f + +count = int(sys.argv[1]) +for n in perfect_iter(): + print(n) + count -= 1 + if count<=0: + break diff --git a/challenge-008/paulo-custodio/python/ch-2.py b/challenge-008/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..fc6bb3acd4 --- /dev/null +++ b/challenge-008/paulo-custodio/python/ch-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +# Challenge 008 +# +# Challenge #2 +# Write a function, 'center', whose argument is a list of strings, which will +# be lines of text. The function should insert spaces at the beginning of the +# lines of text so that if they were printed, the text would be centered, and +# return the modified lines. + +import sys + +def center(lines): + max_len = max([len(x) for x in lines]) + for i in range(len(lines)): + lines[i] = " "*int((max_len-len(lines[i]))/2) + lines[i] + return lines + +lines = center(sys.argv[1:]) +for line in lines: + print(line) diff --git a/challenge-008/paulo-custodio/test.pl b/challenge-008/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-008/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; |
