We define function D(x) as following:
D(x) = x + sum of digits of x + sum of prime factors of x then call x the father of D(x)
Write a program that gets input t
at the first line and then gets an input in the next t
lines. If that input had father, print YES
otherwise NO
For example 12 is the father of 20
20 = 12 + (1 + 2) + (2 + 3)
preferably write a separate function for each of these tasks:
- Getting sum of the digits of a number
- Getting prime factors of a number
- Calculating D(x)
Notice that if you do lots of operations, you may get time limit error.
Time limit: 0.5 seconds
Memory limit: 128 MB
Input:
You get an input number t
at the first line and then in the next t
lines, you get number n
for which you should solve the problem
Output:
Print the answer to each input in t
lines.
Example:
Sample input:
2
4
20
Sample output:
NO
YES
I've written the code with python:
# function that returns the unique prime factors of number n as a list
def prime_factors(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)
factors = list(set(factors))
return factors
# function that returns the sum of digits of number n
def sum_digits(n):
r = 0
while n:
r, n = r + n % 10, n // 10
return r
# function to calculate the offspring of number X {D(X)}
def Offspring(X):
DX = X + sum(prime_factors(X)) + sum_digits(X)
return DX
ChildFather = {i: Offspring(i) for i in range(4, 1001)}
ChildFatherValues = list(set(list(ChildFather.values())))
ChildFather.clear()
Fathers = [i for i in ChildFatherValues if i <= 1000]
ChildFatherValues.clear()
t = eval(input())
for i in range(0, t):
n = eval(input())
if n in Fathers:
print('YES')
else:
print('NO')
I guess this part of code
ChildFather = {i: Offspring(i) for i in range(4, 1001)}
ChildFatherValues = list(set(list(ChildFather.values())))
ChildFather.clear()
Fathers = [i for i in ChildFatherValues if i <= 1000]
ChildFatherValues.clear()
is the bottle-neck for time limit. since I don't know how to find x
by having D(x)
, I've solved the problem this way:
ChildFather = {i: Offspring(i) for i in range(4, 1001)}
for numbers 4<= n<= 1000
, I've created a dictionary {n:D(n)}
ChildFatherValues = list(set(list(ChildFather.values())))
ChildFather.clear()
created a list of unique values of the dictionary and deallocated the memory used for the dictionary
Fathers = [i for i in ChildFatherValues if i <= 1000]
ChildFatherValues.clear()
create a list of values smaller than 1000 and deallocated memory used for the original list
t = eval(input())
for i in range(0, t):
n = eval(input())
if n in Fathers:
print('YES')
else:
print('NO')
If a given number exists n
in the final provided list, I understand that n
can be written as D(m)=m+sum of digits of m + sum of prime factors of m
so n
has the father m
and I'll print Yes
otherwise NO
But seems that the program is not efficient.
- How can I measure the time consumed by this python program at run-time?
- How can I measure the memory consumed by this python program at run-time?
(I'm new to python and am coding with sublime text 3 in Linux Ubuntu) - Is there any more efficient way of writing the code?