Pronic Integers in N - InfyTQ question
Pronic Integers in N - InfyTQ question
Problem:
The program must accept an integer N as the input. The program must print all the pronic integers formed by a series of continuously occurring digits (in the same order) in N as the output. The pronic integers can be represented as n*(n+1).
Input:
93042861
Output:
930 30 0 42 2 6
Program:
import math
s = input().strip()
def check_pronic(num):
sq = math.floor(math.sqrt(num))
return (sq*(sq+1)) == num
l = []
for i in range(len(s)):
for j in range(i,len(s)):
substr = int(s[i:j+1])
if s[i:j+1] == "0":
l.append(substr)
elif s[i:j+1][0]!='0' and check_pronic(substr):
l.append(substr)
print(*l)
Explanation:
1. Accept a string S (a number).
2. Generate all possible substrings.
3. It is obvious from the definition, the pronic integer is formed by the product of two consecutive integers. Therefore, one of the two consecutive integers is approximately equal to the square root of the integer. if (sq * (sq+1)) is equal to integer then it is a pronic integer.
sq = square root of the number.
Special case:
a. 0 is a pronic integer. since 0 = 0*1
b. 02 is not a pronic integer but 2 is a pronic integer. Even if a pronic integer is preceded by a 0, it is not a pronic integer.
Comments
Post a Comment