diff options
25 files changed, 579 insertions, 41 deletions
diff --git a/challenge-148/paulo-custodio/perl/ch-1.pl b/challenge-148/paulo-custodio/perl/ch-1.pl index 2a0ce52919..9dd13818c2 100644 --- a/challenge-148/paulo-custodio/perl/ch-1.pl +++ b/challenge-148/paulo-custodio/perl/ch-1.pl @@ -2,11 +2,11 @@ # Challenge 148 # -# TASK #1 Eban Numbers +# TASK #1 > Eban Numbers # Submitted by: Mohammad S Anwar # Write a script to generate all Eban Numbers <= 100. # -# An Eban number is a number that has no letter e in it when the number +# An Eban number is a number that has no letter 'e' in it when the number # is spelled in English (American or British). # # Example diff --git a/challenge-148/paulo-custodio/perl/ch-2.pl b/challenge-148/paulo-custodio/perl/ch-2.pl index eceb0dc533..2bd9173d42 100644 --- a/challenge-148/paulo-custodio/perl/ch-2.pl +++ b/challenge-148/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 148 # -# TASK #2 Cardano Triplets +# TASK #2 > Cardano Triplets # Submitted by: Mohammad S Anwar # Write a script to generate first 5 Cardano Triplets. # @@ -15,27 +15,47 @@ # (2,1,5) is the first Cardano Triplets. use Modern::Perl; +use List::Util 'sum'; -my $limit = 100; my @triplets = cardano_triplets(5); -for (0..4) { - say "(", join(",", @{$triplets[$_]}), ")"; +for (@triplets) { + say "(", join(",", @$_), ")"; } - sub cardano_triplets { - my($n) = @_; + my($num) = @_; - my @found; - for my $a (1..$limit) { - for my $b (1..$limit) { - for my $c (1..$limit) { - if (8*($a**3)+15*($a**2)+6*$a-27*($b**2)*$c==1) { - push @found, [$a, $b, $c]; - } + my @triplets; + my $K = 0; + while (@triplets < $num*2) { # we are not sure in which order they are generated + my $A = 2+3*$K; + my $T = ($K+1)**2 * (8*$K+5); + my @divs = divisors($T); + for my $div (@divs) { + if (int(sqrt($div)) == sqrt($div)) { + my $B = sqrt($div); + my $C = $T / $B**2; + push @triplets, [$A, $B, $C]; } } + @triplets = sort {sum(@$a) <=> sum(@$b)} @triplets; + + $K++; } - return @found; + return @triplets[0..$num-1]; +} + +sub divisors { + my($n) = @_; + my @divs1; + my @divs2; + for my $k (1 .. $n) { + last if @divs2 && $k >= $divs2[0]; + if ($n % $k == 0) { + push @divs1, $k; + unshift @divs2, $n/$k; + } + } + return (@divs1, @divs2); } diff --git a/challenge-148/paulo-custodio/python/ch-1.py b/challenge-148/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..9417ec6343 --- /dev/null +++ b/challenge-148/paulo-custodio/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +# Challenge 148 +# +# TASK #1 > Eban Numbers +# Submitted by: Mohammad S Anwar +# Write a script to generate all Eban Numbers <= 100. +# +# An Eban number is a number that has no letter 'e' in it when the number +# is spelled in English (American or British). +# +# Example +# 2, 4, 6, 30, 32 are the first 5 Eban numbers. + +from num2words import num2words + +def is_eban(n): + en = num2words(n) + return 'e' not in en + +out = [] +for n in range(1, 101): + if is_eban(n): + out.append(n) + +print(", ".join(map(str, out))) diff --git a/challenge-148/paulo-custodio/python/ch-2.py b/challenge-148/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..73114e1555 --- /dev/null +++ b/challenge-148/paulo-custodio/python/ch-2.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +# Challenge 148 +# +# TASK #2 > Cardano Triplets +# Submitted by: Mohammad S Anwar +# Write a script to generate first 5 Cardano Triplets. +# +# A triplet of positive integers (a,b,c) is called a Cardano Triplet if it +# satisfies the below condition. +# +# Cardano Triplets +# +# Example +# (2,1,5) is the first Cardano Triplets. + +from math import isqrt +from itertools import chain + +def cardano_triplets(num): + triplets = [] + K = 0 + while len(triplets) < num * 2: # we are not sure in which order they are generated + A = 2 + 3 * K + T = (K + 1) ** 2 * (8 * K + 5) + divs = divisors(T) + for div in divs: + if isqrt(div) ** 2 == div: + B = isqrt(div) + C = T // (B ** 2) + triplets.append((A, B, C)) + triplets.sort(key=lambda x: sum(x)) + K += 1 + + return triplets[:num] + +def divisors(n): + divs1 = [] + divs2 = [] + for k in range(1, n + 1): + if divs2 and k >= divs2[0]: + break + if n % k == 0: + divs1.append(k) + divs2.insert(0, n // k) + return list(chain(divs1, divs2)) + +triplets = cardano_triplets(5) +for triplet in triplets: + print(f"({','.join(map(str, triplet))})") diff --git a/challenge-148/paulo-custodio/t/test-2.yaml b/challenge-148/paulo-custodio/t/test-2.yaml index eb8432b926..c2b0ba83bf 100644 --- a/challenge-148/paulo-custodio/t/test-2.yaml +++ b/challenge-148/paulo-custodio/t/test-2.yaml @@ -4,7 +4,7 @@ input: output: | |(2,1,5) - |(5,1,52) |(5,2,13) |(8,3,21) |(11,4,29) + |(14,5,37) diff --git a/challenge-149/paulo-custodio/perl/ch-1.pl b/challenge-149/paulo-custodio/perl/ch-1.pl index 5f4c74432f..48994f7185 100644 --- a/challenge-149/paulo-custodio/perl/ch-1.pl +++ b/challenge-149/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 149 # -# TASK #1 Fibonacci Digit Sum +# TASK #1 > Fibonacci Digit Sum # Submitted by: Roger Bell_West # Given an input $N, generate the first $N numbers for which the sum of their # digits is a Fibonacci number. diff --git a/challenge-149/paulo-custodio/perl/ch-2.pl b/challenge-149/paulo-custodio/perl/ch-2.pl index 3c99608c44..29ffbec4df 100644 --- a/challenge-149/paulo-custodio/perl/ch-2.pl +++ b/challenge-149/paulo-custodio/perl/ch-2.pl @@ -2,10 +2,10 @@ # Challenge 149 # -# TASK #2 Largest Square +# TASK #2 > Largest Square # Submitted by: Roger Bell_West # Given a number base, derive the largest perfect square with no repeated -# digits and return it as a string. (For base>10, use A..Z.) +# digits and return it as a string. (For base>10, use 'A'..'Z'.) # # Example: # f(2)="1" diff --git a/challenge-149/paulo-custodio/python/ch-1.py b/challenge-149/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..f3bd54f69d --- /dev/null +++ b/challenge-149/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +# Challenge 149 +# +# TASK #1 > Fibonacci Digit Sum +# Submitted by: Roger Bell_West +# Given an input $N, generate the first $N numbers for which the sum of their +# digits is a Fibonacci number. +# +# Example +# f(20)=[0, 1, 2, 3, 5, 8, 10, 11, 12, 14, 17, 20, 21, 23, 26, 30, 32, 35, 41, 44] + +import sys +from functools import reduce + +def is_fibonacci(n): + # Function to check if a number is a Fibonacci number + a, b = 0, 1 + while a < n: + a, b = b, a + b + return a == n + +def sum_of_digits_is_fibonacci(n): + digits = [int(d) for d in str(n)] + total = sum(digits) + return is_fibonacci(total) + +count = int(sys.argv[1]) +out = [] +n = 0 +while len(out) < count: + if sum_of_digits_is_fibonacci(n): + out.append(n) + n += 1 + +print(", ".join(map(str, out))) diff --git a/challenge-149/paulo-custodio/python/ch-2.py b/challenge-149/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..b5c900633d --- /dev/null +++ b/challenge-149/paulo-custodio/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# Challenge 149 +# +# TASK #2 > Largest Square +# Submitted by: Roger Bell_West +# Given a number base, derive the largest perfect square with no repeated +# digits and return it as a string. (For base>10, use 'A'..'Z'.) +# +# Example: +# f(2)="1" +# f(4)="3201" +# f(10)="9814072356" +# f(12)="B8750A649321" + +import sys +import math +from itertools import permutations + +def largest_perfect_square(base): + digits = [str(i) for i in range(10)] + [chr(i) for i in range(ord('A'), ord('A') + 26)] + digits = digits[:base] + + max_num = 0 + max_str = "0" + + for permu in permutations(digits, base): + str_num = ''.join(permu) + str_num = str_num.lstrip('0') or '0' + num = int(str_num, base) + + if math.isqrt(num) ** 2 == num: # is perfect square + if num > max_num: + max_num, max_str = num, str_num + + return max_str + +base = int(sys.argv[1]) +print(largest_perfect_square(base)) diff --git a/challenge-150/paulo-custodio/perl/ch-1.pl b/challenge-150/paulo-custodio/perl/ch-1.pl index 29886086e3..4d68df8d9c 100644 --- a/challenge-150/paulo-custodio/perl/ch-1.pl +++ b/challenge-150/paulo-custodio/perl/ch-1.pl @@ -2,21 +2,31 @@ # Challenge 150 # -# Input: $a = '1234' $b = '5678' -# Output: 7 +# TASK #1 > Fibonacci Words +# Submitted by: Mohammad S Anwar # -# Fibonacci Words: +# You are given two strings having same number of digits, $a and $b. # -# '1234' -# '5678' -# '12345678' -# '567812345678' -# '12345678567812345678' -# '56781234567812345678567812345678' -# '1234567856781234567856781234567812345678567812345678' +# Write a script to generate Fibonacci Words by concatenation of the previous +# two strings. Finally print 51st digit of the first term having at least +# 51 digits. +# Example: # -# The 51st digit in the first term having at least 51 digits -# '1234567856781234567856781234567812345678567812345678' is 7. +# Input: $a = '1234' $b = '5678' +# Output: 7 +# +# Fibonacci Words: +# +# '1234' +# '5678' +# '12345678' +# '567812345678' +# '12345678567812345678' +# '56781234567812345678567812345678' +# '1234567856781234567856781234567812345678567812345678' +# +# The 51st digit in the first term having at least 51 digits +# '1234567856781234567856781234567812345678567812345678' is 7. use Modern::Perl; diff --git a/challenge-150/paulo-custodio/perl/ch-2.pl b/challenge-150/paulo-custodio/perl/ch-2.pl index 87d688a504..73fb98b9a4 100644 --- a/challenge-150/paulo-custodio/perl/ch-2.pl +++ b/challenge-150/paulo-custodio/perl/ch-2.pl @@ -2,14 +2,14 @@ # Challenge 150 # -# TASK #2 Square-free Integer +# TASK #2 > Square-free Integer # Submitted by: Mohammad S Anwar # Write a script to generate all square-free integers <= 500. # # In mathematics, a square-free integer (or squarefree integer) is an integer # which is divisible by no perfect square other than 1. That is, its prime # factorization has exactly one factor for each prime that appears in it. For -# example, 10 = 2 · 5 is square-free, but 18 = 2 · 3 · 3 is not, because 18 is +# example, 10 = 2 x 5 is square-free, but 18 = 2 x 3 x 3 is not, because 18 is # divisible by 9 = 3**2. # # Example diff --git a/challenge-150/paulo-custodio/python/ch-1.py b/challenge-150/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..b3b832d0a2 --- /dev/null +++ b/challenge-150/paulo-custodio/python/ch-1.py @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 150 +# +# TASK #1 > Fibonacci Words +# Submitted by: Mohammad S Anwar +# +# You are given two strings having same number of digits, $a and $b. +# +# Write a script to generate Fibonacci Words by concatenation of the previous +# two strings. Finally print 51st digit of the first term having at least +# 51 digits. +# Example: +# +# Input: $a = '1234' $b = '5678' +# Output: 7 +# +# Fibonacci Words: +# +# '1234' +# '5678' +# '12345678' +# '567812345678' +# '12345678567812345678' +# '56781234567812345678567812345678' +# '1234567856781234567856781234567812345678567812345678' +# +# The 51st digit in the first term having at least 51 digits +# '1234567856781234567856781234567812345678567812345678' is 7. + +import sys + +pos = 51 + +def fib_word(a, b, length): + seq = [a, b] + while len(seq[-1]) <= length: + seq.append(seq[-2] + seq[-1]) + seq.pop(0) + return seq[-1] + +words = sys.argv[1:3] +fib_word_result = fib_word(words[0], words[1], pos) +print(fib_word_result[pos - 1]) diff --git a/challenge-150/paulo-custodio/python/ch-2.py b/challenge-150/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..bdfb6bc4ae --- /dev/null +++ b/challenge-150/paulo-custodio/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +# Challenge 150 +# +# TASK #2 > Square-free Integer +# Submitted by: Mohammad S Anwar +# Write a script to generate all square-free integers <= 500. +# +# In mathematics, a square-free integer (or squarefree integer) is an integer +# which is divisible by no perfect square other than 1. That is, its prime +# factorization has exactly one factor for each prime that appears in it. For +# example, 10 = 2 x 5 is square-free, but 18 = 2 x 3 x 3 is not, because 18 is +# divisible by 9 = 3**2. +# +# Example +# The smallest positive square-free integers are +# 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, ... + +from sympy import factorint + +def is_squarefree(n): + factors = factorint(n) + for k in factors.values(): + if k > 1: + return False + return True + +out = [n for n in range(1, 501) if is_squarefree(n)] +print(", ".join(map(str, out))) diff --git a/challenge-151/paulo-custodio/perl/ch-1.pl b/challenge-151/paulo-custodio/perl/ch-1.pl index 09184918b8..9b265b16ac 100644 --- a/challenge-151/paulo-custodio/perl/ch-1.pl +++ b/challenge-151/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 151 # -# TASK #1 Binary Tree Depth +# TASK #1 > Binary Tree Depth # Submitted by: Mohammad S Anwar # You are given binary tree. # diff --git a/challenge-151/paulo-custodio/perl/ch-2.pl b/challenge-151/paulo-custodio/perl/ch-2.pl index 3581a4c94c..719fb7d336 100644 --- a/challenge-151/paulo-custodio/perl/ch-2.pl +++ b/challenge-151/paulo-custodio/perl/ch-2.pl @@ -2,10 +2,10 @@ # Challenge 151 # -# TASK #2 Rob The House +# TASK #2 > Rob The House # Submitted by: Mohammad S Anwar # You are planning to rob a row of houses, always starting with the first -# and moving in the same direction. However, you cant rob two adjacent houses. +# and moving in the same direction. However, you can't rob two adjacent houses. # # Write a script to find the highest possible gain that can be achieved. # diff --git a/challenge-151/paulo-custodio/python/ch-1.py b/challenge-151/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..ede0f99b16 --- /dev/null +++ b/challenge-151/paulo-custodio/python/ch-1.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +# Challenge 151 +# +# TASK #1 > Binary Tree Depth +# Submitted by: Mohammad S Anwar +# You are given binary tree. +# +# Write a script to find the minimum depth. +# +# The minimum depth is the number of nodes from the root to the nearest leaf +# node (node without any children). +# +# Example 1: +# Input: '1 | 2 3 | 4 5' +# +# 1 +# / \ +# 2 3 +# / \ +# 4 5 +# +# Output: 2 +# +# Example 2: +# Input: '1 | 2 3 | 4 * * 5 | * 6' +# +# 1 +# / \ +# 2 3 +# / \ +# 4 5 +# \ +# 6 +# Output: 3 + +import fileinput +import re +import sys + +class Node: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + def __repr__(self): + return "Node(value: {}, left: {}, right: {})" \ + .format(self.value, self.left, self.right) + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +def parse_subtree(lines, row, col): + def ch(row, col): + if row < 0 or row >= len(lines) or \ + col < 0 or col >= len(lines[row]): + return ' ' + else: + return lines[row][col] + + tree = Node(int(lines[row][col])) + if ch(row + 1, col - 1) == '/': + tree.left = parse_subtree(lines, row + 2, col - 2) + if ch(row + 1, col + 1) == '\\': + tree.right = parse_subtree(lines, row + 2, col + 2) + + return tree + +def parse_tree(lines): + found = re.search(r'^[ ]+\d', lines[0]) + col = found.span()[1] - 1 + return parse_subtree(lines, 0, col) + +def min_depth(tree): + if not tree: + return 0 + else: + return 1+min(min_depth(tree.left), min_depth(tree.right)) + +tree = parse_tree(read_input()) +print(min_depth(tree)) diff --git a/challenge-151/paulo-custodio/python/ch-2.py b/challenge-151/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..ad18d07d6c --- /dev/null +++ b/challenge-151/paulo-custodio/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +# Challenge 151 +# +# TASK #2 > Rob The House +# Submitted by: Mohammad S Anwar +# You are planning to rob a row of houses, always starting with the first +# and moving in the same direction. However, you can't rob two adjacent houses. +# +# Write a script to find the highest possible gain that can be achieved. +# +# Example 1: +# Input: @valuables = (2, 4, 5); +# Output: 7 +# +# If we rob house (index=0) we get 2 and then the only house we can rob is +# house (index=2) where we have 5. +# So the total valuables in this case is (2 + 5) = 7. +# +# Example 2: +# Input: @valuables = (4, 2, 3, 6, 5, 3); +# Output: 13 +# +# The best choice would be to first rob house (index=0) then rob house (index=3) then finally house (index=5). +# This would give us 4 + 6 + 3 =13. + +import sys + +def max_gain(valuables): + gain = valuables[0] + max_next = 0 + for i in range(2, len(valuables)): + next = max_gain(valuables[i:]) + max_next = max(max_next, next) + return gain+max_next + +valuables = list(map(int, sys.argv[1:])) +print(max_gain(valuables)) diff --git a/challenge-152/paulo-custodio/perl/ch-1.pl b/challenge-152/paulo-custodio/perl/ch-1.pl index 91d309f516..604c851859 100644 --- a/challenge-152/paulo-custodio/perl/ch-1.pl +++ b/challenge-152/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 152 # -# TASK #1 Triangle Sum Path +# TASK #1 > Triangle Sum Path # Submitted by: Mohammad S Anwar # You are given a triangle array. # diff --git a/challenge-152/paulo-custodio/perl/ch-2.pl b/challenge-152/paulo-custodio/perl/ch-2.pl index 6a5b8bc0e8..9d2258ead8 100644 --- a/challenge-152/paulo-custodio/perl/ch-2.pl +++ b/challenge-152/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 152 # -# TASK #2 Rectangle Area +# TASK #2 > Rectangle Area # Submitted by: Mohammad S Anwar # You are given coordinates bottom-left and top-right corner of two rectangles # in a 2D plane. diff --git a/challenge-152/paulo-custodio/python/ch-1.py b/challenge-152/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..c454f06f43 --- /dev/null +++ b/challenge-152/paulo-custodio/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +# Challenge 152 +# +# TASK #1 > Triangle Sum Path +# Submitted by: Mohammad S Anwar +# You are given a triangle array. +# +# Write a script to find the minimum sum path from top to bottom. +# +# Example 1: +# Input: $triangle = [ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ] +# +# 1 +# 5 3 +# 2 3 4 +# 7 1 0 2 +# 6 4 5 2 8 +# +# Output: 8 +# +# Minimum Sum Path = 1 + 3 + 2 + 0 + 2 => 8 +# Example 2: +# Input: $triangle = [ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9] ] +# +# 5 +# 2 3 +# 4 1 5 +# 0 1 2 3 +# 7 2 4 1 9 +# +# Output: 9 +# +# Minimum Sum Path = 5 + 2 + 1 + 0 + 1 => 9 + +import fileinput + +def read_input(): + lines = [] + for line in fileinput.input(): + cols = [int(x) for x in line.split()] + lines.append(cols) + return lines + +def min_sum_path(lines): + path = [min(x) for x in lines] + return sum(path) + +lines = read_input() +print(min_sum_path(lines)) diff --git a/challenge-152/paulo-custodio/python/ch-2.py b/challenge-152/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..df732c6506 --- /dev/null +++ b/challenge-152/paulo-custodio/python/ch-2.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +# Challenge 152 +# +# TASK #2 > Rectangle Area +# Submitted by: Mohammad S Anwar +# You are given coordinates bottom-left and top-right corner of two rectangles +# in a 2D plane. +# +# Write a script to find the total area covered by the two rectangles. +# +# Example 1: +# Input: Rectangle 1 => (-1,0), (2,2) +# Rectangle 2 => (0,-1), (4,4) +# +# Output: 22 +# Example 2: +# Input: Rectangle 1 => (-3,-1), (1,3) +# Rectangle 2 => (-1,-3), (2,2) +# +# Output: 25 + +import sys + +x1, y1, x2, y2, x3, y3, x4, y4 = map(int, sys.argv[1:]) + +# area of rectangles +area12 = abs(x2-x1)*abs(y2-y1) +area34 = abs(x4-x3)*abs(y4-y3) + +# intersection of rectangles +x5 = max(x1, x3) +y5 = max(y1, y3) +x6 = min(x2, x4) +y6 = min(y2, y4) + +area56 = abs(x6-x5)*abs(y6-y5) + +if x5 > x6 or y5 > y6: # no intersection + area56 = 0 + +area = area12 + area34 - area56 + +print(area) diff --git a/challenge-153/paulo-custodio/perl/ch-1.pl b/challenge-153/paulo-custodio/perl/ch-1.pl index 29f2d4ed37..07f81c2860 100644 --- a/challenge-153/paulo-custodio/perl/ch-1.pl +++ b/challenge-153/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 153 # -# TASK #1 Left Factorials +# TASK #1 > Left Factorials # Submitted by: Mohammad S Anwar # Write a script to compute Left Factorials of 1 to 10. Please refer # OEIS A003422 for more information. diff --git a/challenge-153/paulo-custodio/perl/ch-2.pl b/challenge-153/paulo-custodio/perl/ch-2.pl index 95631b0e51..e87e4bd435 100644 --- a/challenge-153/paulo-custodio/perl/ch-2.pl +++ b/challenge-153/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 153 # -# TASK #2 Factorions +# TASK #2 > Factorions # Submitted by: Mohammad S Anwar # You are given an integer, $n. # diff --git a/challenge-153/paulo-custodio/python/ch-1.py b/challenge-153/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..054f47cfb3 --- /dev/null +++ b/challenge-153/paulo-custodio/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +# Challenge 153 +# +# TASK #1 > Left Factorials +# Submitted by: Mohammad S Anwar +# Write a script to compute Left Factorials of 1 to 10. Please refer +# OEIS A003422 for more information. +# +# Expected Output: +# 1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114 + +def fact(n): + if n < 2: + return 1 + else: + return n * fact(n-1) + +def left_fact(n): + sum = 0 + for k in range(n): + sum += fact(k) + return sum + +out = [left_fact(x) for x in range(1, 10+1)] +print(", ".join(map(str, out))) diff --git a/challenge-153/paulo-custodio/python/ch-2.py b/challenge-153/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..cafb749fac --- /dev/null +++ b/challenge-153/paulo-custodio/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# Challenge 153 +# +# TASK #2 > Factorions +# Submitted by: Mohammad S Anwar +# You are given an integer, $n. +# +# Write a script to figure out if the given integer is factorion. +# +# A factorion is a natural number that equals the sum of the factorials +# of its digits. +# +# Example 1: +# Input: $n = 145 +# Output: 1 +# +# Since 1! + 4! + 5! => 1 + 24 + 120 = 145 +# Example 2: +# Input: $n = 123 +# Output: 0 +# +# Since 1! + 2! + 3! => 1 + 2 + 6 <> 123 + +import sys + +def fact(n): + if n < 2: + return 1 + else: + return n * fact(n-1) + +def is_factorian(n): + fact_sum = [fact(int(x)) for x in str(n)] + if sum(fact_sum) == n: + return True + else: + return False + +n = int(sys.argv[1]) +print(1 if is_factorian(n) else 0) |
