Python Program to find the nth Kynea number
In this article, we will learn how to find the nth Kynea number in Python. But first, let’s discuss a few points about Kynea number.
What is Kynea number?
In mathematical, Kynea number is an integer of the form
where n is a positive integer. Which is equivalent to . To learn more about Kynea number click here.
The first few Kynea numbers are
7, 23, 79, 287, 1087, 4223, 16639,…………..
Find the nth Kynea number in Python
Method 1:
Directly applying the formulae to given input n. Python has an inbuilt function pow() to calculate the power of any value.
def kynea_number(n):
result = 0
result = (pow(2,n)+1)**2 - 2
return result
n = int(input("Enter the n value: "))
print("The nth kynea number is ", kynea_number(n))Output
Enter the n value: 8 The nth kynea number is 66047 Enter the n value: 21 The nth kynea number is 4398050705407
Time complexity: O(1)
Space complexity: O(1)
Method 2: Using Bitwise shift
The value of 2^n is equivalent to 1<<n. So we are going to use 1<<n instead of 2^n and apply the formulae .
def kynea_number(n):
# calculate 2^n+1
n = (1<<n)+1
# (2^n+1)^2
n = n*n
# ((2^n+1)^2)-2
n = n-2
return n
n = int(input("Enter the n value: "))
print("The nth kynea number is ", kynea_number(n))Output
Enter the n value: 7 The nth kynea number is 16639 Enter the n value: 6 The nth kynea number is 4223
Time complexity: O(1)
Space complexity: O(1)
Also, read
Leave a Reply