How to resolve the algorithm Attractive numbers step by step in the Python programming language
How to resolve the algorithm Attractive numbers step by step in the Python programming language
Table of Contents
Problem Statement
A number is an attractive number if the number of its prime factors (whether distinct or not) is also prime.
The number 20, whose prime decomposition is 2 × 2 × 5, is an attractive number because the number of its prime factors (3) is also prime.
Show sequence items up to 120.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Attractive numbers step by step in the Python programming language
The first part of the code gets the prime factorization of the numbers from 0 to 120, and stores the length of the factorization in a list called pool
.
Then it iterates over the list and prints the numbers that are prime and have a prime number of factors.
The second part of the code implements an attractive number generator.
An attractive number is a number whose prime factorization has a prime number of factors. The function attractiveNumbers
generates the stream of attractive numbers.
The function isPrime
checks if a number is prime.
The function primeDecomposition
returns the list of the prime factors of a number.
The function chunksOf
divides a list into sublists of a given length.
The function compose
composes a list of functions from right to left.
The function justifyRight
right-justifies a string with a given padding character.
The main function generates the list of the first 120 attractive numbers and prints them in chunks of 15.
Source code in the python programming language
from sympy import sieve # library for primes
def get_pfct(n):
i = 2; factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return len(factors)
sieve.extend(110) # first 110 primes...
primes=sieve._list
pool=[]
for each in xrange(0,121):
pool.append(get_pfct(each))
for i,each in enumerate(pool):
if each in primes:
print i,
'''Attractive numbers'''
from itertools import chain, count, takewhile
from functools import reduce
# attractiveNumbers :: () -> [Int]
def attractiveNumbers():
'''A non-finite stream of attractive numbers.
(OEIS A063989)
'''
return filter(
compose(
isPrime,
len,
primeDecomposition
),
count(1)
)
# TEST ----------------------------------------------------
def main():
'''Attractive numbers drawn from the range [1..120]'''
for row in chunksOf(15)(list(
takewhile(
lambda x: 120 >= x,
attractiveNumbers()
)
)):
print(' '.join(map(
compose(justifyRight(3)(' '), str),
row
)))
# GENERAL FUNCTIONS ---------------------------------------
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''
return lambda xs: reduce(
lambda a, i: a + [xs[i:n + i]],
range(0, len(xs), n), []
) if 0 < n else []
# compose :: ((a -> a), ...) -> (a -> a)
def compose(*fs):
'''Composition, from right to left,
of a series of functions.
'''
return lambda x: reduce(
lambda a, f: f(a),
fs[::-1], x
)
# We only need light implementations
# of prime functions here:
# primeDecomposition :: Int -> [Int]
def primeDecomposition(n):
'''List of integers representing the
prime decomposition of n.
'''
def go(n, p):
return [p] + go(n // p, p) if (
0 == n % p
) else []
return list(chain.from_iterable(map(
lambda p: go(n, p) if isPrime(p) else [],
range(2, 1 + n)
)))
# isPrime :: Int -> Bool
def isPrime(n):
'''True if n is prime.'''
if n in (2, 3):
return True
if 2 > n or 0 == n % 2:
return False
if 9 > n:
return True
if 0 == n % 3:
return False
return not any(map(
lambda x: 0 == n % x or 0 == n % (2 + x),
range(5, 1 + int(n ** 0.5), 6)
))
# justifyRight :: Int -> Char -> String -> String
def justifyRight(n):
'''A string padded at left to length n,
using the padding character c.
'''
return lambda c: lambda s: s.rjust(n, c)
# MAIN ---
if __name__ == '__main__':
main()
You may also check:How to resolve the algorithm Reduced row echelon form step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Compare length of two strings step by step in the RPL programming language
You may also check:How to resolve the algorithm Speech synthesis step by step in the Groovy programming language
You may also check:How to resolve the algorithm File size step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Documentation step by step in the SQL PL programming language