diff options
| author | 冯昶 <seaker@qq.com> | 2021-10-26 14:25:00 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-10-26 14:25:00 +0800 |
| commit | 8ea3219dc48768220f95c2bac863eb542c5dba37 (patch) | |
| tree | fa90638ec349452cd3ea59a41d595dbd7fc45f6b /challenge-133 | |
| parent | fb866d6d23493558f190fcdbd9a4ec165a6c1f98 (diff) | |
| parent | d4af640c5e8baf60dcff6c3e994bad84ad67a4a2 (diff) | |
| download | perlweeklychallenge-club-8ea3219dc48768220f95c2bac863eb542c5dba37.tar.gz perlweeklychallenge-club-8ea3219dc48768220f95c2bac863eb542c5dba37.tar.bz2 perlweeklychallenge-club-8ea3219dc48768220f95c2bac863eb542c5dba37.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-133')
| -rw-r--r-- | challenge-133/conor-hoekstra/README | 1 | ||||
| -rw-r--r-- | challenge-133/paulo-custodio/python/ch-2.py | 109 |
2 files changed, 61 insertions, 49 deletions
diff --git a/challenge-133/conor-hoekstra/README b/challenge-133/conor-hoekstra/README new file mode 100644 index 0000000000..d04e921681 --- /dev/null +++ b/challenge-133/conor-hoekstra/README @@ -0,0 +1 @@ +Solutions by Conor Hoekstra. 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 |
