Given two numbers N and K, the task is to check if N is a K-Persistent Number or not. If N is a K-Persistent Number then print “Yes” else print “No”.
A number N is called K-Persistent Number if all of {n, 2n,…, kn} are Pandigital number in base 10.
Examples :
Input : N = 1023456789, K = 2 Output : Yes Explanation : 1023456789 * 1 = 1023456789 = Pandigital number in base 10 1023456789 * 2 = 2046913578 = Pandigital number in base 10 Input : N = 561, K = 1 Output : No
Approach :
The idea is simple.We iterate through all numbers i from 1 to k and for every integer i in the loop, we check if i*n is a Pandigital number in base 10 or not. If at any i it’s i*n is not Pandigital number in base 10 the return false, otherwise return true at last.
Implementation :
# your code goes here
# python3 program for checking K-Persistent Number
import math
# Return true if n is pandigital in base 10 else return false.
def isPandigital(n):
n = str(n)
b = 10
hash = [0] * b;
# Traversing each digit of the number.
for i in range(len(n)):
# If digit is integer
if (n[i] >= '0' and n[i] <= '9'):
hash[ord(n[i]) - ord('0')] = 1;
# If digit is alphabet
elif (ord(n[i]) - ord('A') <= b - 11):
hash[ord(n[i]) - ord('A') + 10] = 1;
# Checking hash array, if any index is
# unmarked.
for i in range(b):
if (hash[i] == 0):
return False
return True
# Returns true if N is a K-Persistent Number
def isPersistentNumber(N, K):
for i in range(1, K+1):
if(isPandigital(i*N)==False):
return False
return True
# Driver code
# Given Number N
N = 1052674893
# Given Number K
K = 3
# Function Call
if (isPersistentNumber(N, K)):
print("Yes")
else:
print("No")