diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-20 10:00:45 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-21 17:51:10 +0100 |
| commit | 3a258964c8719b343d121d21e43f32c44b314756 (patch) | |
| tree | c5eb31353366b15049eeb7240ccba828994323f3 | |
| parent | 20c6e9bc78a2bab9f8da2f0bda2fa204a6ffdb3c (diff) | |
| download | perlweeklychallenge-club-3a258964c8719b343d121d21e43f32c44b314756.tar.gz perlweeklychallenge-club-3a258964c8719b343d121d21e43f32c44b314756.tar.bz2 perlweeklychallenge-club-3a258964c8719b343d121d21e43f32c44b314756.zip | |
Remove dependency from sympi
| -rw-r--r-- | challenge-133/paulo-custodio/python/ch-2.py | 109 |
1 files changed, 60 insertions, 49 deletions
diff --git a/challenge-133/paulo-custodio/python/ch-2.py b/challenge-133/paulo-custodio/python/ch-2.py index e03c74f7ab..1cab0e4e43 100644 --- a/challenge-133/paulo-custodio/python/ch-2.py +++ b/challenge-133/paulo-custodio/python/ch-2.py @@ -1,49 +1,60 @@ -#!/usr/bin/env python3
-
-# TASK #2 > Smith Numbers
-# Submitted by: Mohammad S Anwar
-# Write a script to generate first 10 Smith Numbers in base 10.
-#
-# According to Wikipedia:
-#
-# In number theory, a Smith number is a composite number for which, in a given
-# number base, the sum of its digits is equal to the sum of the digits in its
-# prime factorization in the given number base.
-#
-
-from sympy import isprime
-
-def get_prime_factors(n):
- i = 2
- prime_factors = []
- while i*i <= n:
- if n%i == 0:
- prime_factors.append(i)
- n //= i
- else:
- i += 1
-
- if n>1:
- prime_factors.append(n)
-
- return prime_factors
-
-def is_smith(n):
- if isprime(n):
- return False
- digits = [int(x) for x in str(n)]
- sum1 = sum(digits)
- factors = ''.join([str(x) for x in get_prime_factors(n)])
- fact_digits = [int(x) for x in factors]
- sum2 = sum(fact_digits)
- return sum1==sum2
-
-n=1
-count=0
-while count<10:
- n+=1
- if is_smith(n):
- print(n)
- count+=1
-
-
\ No newline at end of file +#!/usr/bin/env python3 + +# TASK #2 > Smith Numbers +# Submitted by: Mohammad S Anwar +# Write a script to generate first 10 Smith Numbers in base 10. +# +# According to Wikipedia: +# +# In number theory, a Smith number is a composite number for which, in a given +# number base, the sum of its digits is equal to the sum of the digits in its +# prime factorization in the given number base. +# + +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 get_prime_factors(n): + i = 2 + prime_factors = [] + while i*i <= n: + if n%i == 0: + prime_factors.append(i) + n //= i + else: + i += 1 + + if n>1: + prime_factors.append(n) + + return prime_factors + +def is_smith(n): + if is_prime(n): + return False + digits = [int(x) for x in str(n)] + sum1 = sum(digits) + factors = ''.join([str(x) for x in get_prime_factors(n)]) + fact_digits = [int(x) for x in factors] + sum2 = sum(fact_digits) + return sum1==sum2 + +n=1 +count=0 +while count<10: + n+=1 + if is_smith(n): + print(n) + count+=1 |
