text
stringlengths
1
636
code
stringlengths
8
1.89k
Function to check if N is a pentadecagon number
def isPentadecagon ( N ) : NEW_LINE INDENT n = ( 11 + sqrt ( 104 * N + 121 ) ) / 26 ; NEW_LINE DEDENT
Condition to check if the number is a pentadecagon number
return ( n - int ( n ) == 0 ) ; NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Given number
N = 15 ; NEW_LINE
Function call
if ( isPentadecagon ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Python3 program for the above approach
import math NEW_LINE
Function to check if N is a Tetradecagonal Number
def istetradecagonal ( N ) : NEW_LINE INDENT n = ( 10 + math . sqrt ( 96 * N + 100 ) ) / 24 NEW_LINE DEDENT
Condition to check if the number is a tetradecagonal number
if ( n - int ( n ) ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE
Given Number
N = 11 NEW_LINE
Function call
if ( istetradecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Function to calculate the N - th Icosagonal number
def Icosagonal_num ( n ) : NEW_LINE
Formula to calculate nth Icosagonal number & return it
return ( 18 * n * n - 16 * n ) // 2 NEW_LINE
Function to find the sum of the first N Icosagonal numbers
def sum_Icosagonal_num ( n ) : NEW_LINE
Variable to store the sum
summ = 0 NEW_LINE
Loop to iterate through the first N values and find the sum of first N Icosagonal numbers
for i in range ( 1 , n + 1 ) : NEW_LINE
function to get the Icosagonal_num
summ += Icosagonal_num ( i ) NEW_LINE return summ NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE DEDENT
Display the sum of first N Icosagonal number
print ( sum_Icosagonal_num ( n ) ) NEW_LINE
Function to find the Centered_Pentagonal number
def Centered_Pentagonal_num ( n ) : NEW_LINE
Formula to calculate nth Centered_Pentagonal number & return it into main function .
return ( 5 * n * n - 5 * n + 2 ) // 2 NEW_LINE
Function to find the sum of the first N Centered_Pentagonal numbers
def sum_Centered_Pentagonal_num ( n ) : NEW_LINE
To get the sum
summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE
Function to get the Centered_Pentagonal_num
summ += Centered_Pentagonal_num ( i ) NEW_LINE return summ NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE DEDENT
display first Nth Centered_Pentagonal number
print ( sum_Centered_Pentagonal_num ( n ) ) NEW_LINE
Function to calculate the N - th Centered tridecagonal number
def Centered_tridecagonal_num ( n ) : NEW_LINE
Formula to calculate Nth Centered tridecagonal number & return it
return ( 13 * n * ( n - 1 ) + 2 ) // 2 NEW_LINE
Function to find the sum of the first N Centered tridecagonal numbers
def sum_Centered_tridecagonal_num ( n ) : NEW_LINE
Variable to store the sum
summ = 0 NEW_LINE
Loop to iterate and find the sum of first N Centered tridecagonal numbers
for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_tridecagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_tridecagonal_num ( n ) ) NEW_LINE DEDENT
Python3 program to check if N is a concentric hexagonal number
import math NEW_LINE
Function to check if the number is a concentric hexagonal number
def isConcentrichexagonal ( N ) : NEW_LINE INDENT n = math . sqrt ( ( 2 * N ) / 3 ) NEW_LINE DEDENT
Condition to check if the number is a concentric hexagonal number
return ( n - int ( n ) ) == 0 NEW_LINE
Driver code
N = 6 NEW_LINE
Function call
if isConcentrichexagonal ( N ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
A utility function that find the Prime Numbers till N
def computePrime ( N ) : NEW_LINE
Resize the Prime Number
Prime = [ True ] * ( N + 1 ) NEW_LINE Prime [ 0 ] = False NEW_LINE Prime [ 1 ] = False NEW_LINE
Loop till sqrt ( N ) to find prime numbers and make their multiple false in the bool array Prime
i = 2 NEW_LINE while i * i <= N : NEW_LINE INDENT if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N , i ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return Prime NEW_LINE
Function that returns the count of SPP ( Sexy Prime Pair ) Pairs
def countSexyPairs ( arr , n ) : NEW_LINE
Find the maximum element in the given array arr [ ]
maxE = max ( arr ) NEW_LINE
Function to calculate the prime numbers till N
Prime = computePrime ( maxE ) NEW_LINE
To store the count of pairs
count = 0 NEW_LINE
To store the frequency of element in the array arr [ ]
freq = [ 0 ] * ( maxE + 6 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT
Sort before traversing the array
arr . sort ( ) NEW_LINE
Traverse the array and find the pairs with SPP ( Sexy Prime Pair ) s
for i in range ( n ) : NEW_LINE
If current element is Prime , then check for ( current element + 6 )
if ( Prime [ arr [ i ] ] ) : NEW_LINE INDENT if ( ( arr [ i ] + 6 ) <= ( maxE ) and freq [ arr [ i ] + 6 ] > 0 and Prime [ arr [ i ] + 6 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT
Return the count of pairs
return count NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 7 , 5 , 11 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE DEDENT
Function call to find SPP ( Sexy Prime Pair ) s pair
print ( countSexyPairs ( arr , n ) ) NEW_LINE
Function to find the number of ways
def countWays ( N ) : NEW_LINE
Check if number is less than 2
if ( N <= 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE
Calculate the sum
ans = ( N - 1 ) * ( N - 2 ) / 2 NEW_LINE print ( ans ) NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE countWays ( N ) NEW_LINE DEDENT
Python3 implementation to check that a integer is a power of two
import math NEW_LINE
Function to check if the number is a power of two
def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( math . ceil ( math . log ( n ) // math . log ( 2 ) ) == math . floor ( math . log ( n ) // math . log ( 2 ) ) ) ; NEW_LINE DEDENT
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE if isPowerOfTwo ( N ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Function to count the pairs
def count_pairs ( x ) : NEW_LINE
Initializing answer with 1
ans = 1 ; NEW_LINE
Iterating through bits of x
while ( x > 0 ) : NEW_LINE
Check if bit is 1
if ( x % 2 == 1 ) : NEW_LINE
Multiplying ans by 3 if bit is 1
ans = ans * 3 ; NEW_LINE x = x // 2 ; NEW_LINE return ans ; NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 6 ; NEW_LINE print ( count_pairs ( X ) ) ; NEW_LINE DEDENT
Python3 implementation for above approach
import sys NEW_LINE
Function to find the Kth not divisible by N
def kthNonDivisible ( N , K ) : NEW_LINE
Lowest possible value
L = 1 NEW_LINE
Highest possible value
H = sys . maxsize NEW_LINE
To store the Kth non divisible number of N
ans = 0 NEW_LINE
Using binary search
while ( L <= H ) : NEW_LINE
Calculating mid value
mid = ( L + H ) // 2 NEW_LINE
Sol would have the value by subtracting all multiples of n till mid
sol = mid - mid // N NEW_LINE
Check if sol is greater than k
if ( sol > K ) : NEW_LINE
H should be reduced to find minimum possible value
H = mid - 1 NEW_LINE
Check if sol is less than k then L will be mid + 1
elif ( sol < K ) : NEW_LINE L = mid + 1 NEW_LINE
Check if sol is equal to k
else : NEW_LINE
ans will be mid
ans = mid NEW_LINE
H would be reduced to find any more possible value
H = mid - 1 NEW_LINE
Print the answer
print ( ans ) NEW_LINE
Driver Code
N = 3 NEW_LINE K = 7 NEW_LINE
Function call
kthNonDivisible ( N , K ) NEW_LINE
Function to print the required pair
def printPair ( n ) : NEW_LINE
Print the pair
print ( "1" , end = " ▁ " ) NEW_LINE print ( n - 1 ) NEW_LINE
Driver code
n = 14 NEW_LINE printPair ( n ) NEW_LINE
Function to check number is autobiographical
def isAutoBiographyNum ( number ) : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT
Convert integer to string
NUM = str ( number ) ; NEW_LINE size = len ( NUM ) ; NEW_LINE
Iterate for every digit to check for their total count
for i in range ( size ) : NEW_LINE INDENT position = ord ( NUM [ i ] ) - ord ( '0' ) ; NEW_LINE count = 0 ; NEW_LINE DEDENT
Check occurrence of every number and count them
for j in range ( size ) : NEW_LINE INDENT digit = ord ( NUM [ j ] ) - ord ( '0' ) ; NEW_LINE if ( digit == i ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT
Check if any position mismatches with total count them return with false else continue with loop
if ( position != count ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT return True ; NEW_LINE
Function to return the length of the largest subarray whose every element is a autobiographical number
def checkArray ( arr , n ) : NEW_LINE INDENT current_length = 0 ; NEW_LINE max_length = 0 ; NEW_LINE DEDENT
Utility function which checks every element of array for autobiographical number
for i in range ( n ) : NEW_LINE
Check if element arr [ i ] is an autobiographical number
if ( isAutoBiographyNum ( arr [ i ] ) ) : NEW_LINE
Increment the current length
current_length += 1 ; NEW_LINE else : NEW_LINE current_length = 0 ; NEW_LINE
Update max_length value
max_length = max ( max_length , current_length ) ; NEW_LINE
Return the final result
return max_length ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 21200 , 1 , 1303 , 1210 , 2020 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( checkArray ( arr , n ) ) ; NEW_LINE DEDENT
Python3 implementation of nodes at prime height in the given tree
MAX = 100000 NEW_LINE graph = [ [ ] for i in range ( MAX + 1 ) ] NEW_LINE
To store Prime Numbers
Prime = [ True for i in range ( MAX + 1 ) ] NEW_LINE
To store height of each node
height = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE
Function to find the prime numbers till 10 ^ 5
def SieveOfEratosthenes ( ) : NEW_LINE INDENT Prime [ 0 ] = Prime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i <= MAX : NEW_LINE DEDENT
Traverse all multiple of i and make it false
if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , MAX , i ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE