text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Initialize previous_prime to n - 1 and next_prime to n + 1 | previous_prime = n - 1 NEW_LINE next_prime = n + 1 NEW_LINE |
Find next prime number | while not isPrime ( next_prime ) : NEW_LINE INDENT next_prime += 1 NEW_LINE DEDENT |
Find previous prime number | while not isPrime ( previous_prime ) : NEW_LINE INDENT previous_prime -= 1 NEW_LINE DEDENT |
Arithmetic mean | mean = ( previous_prime + next_prime ) / 2 NEW_LINE |
If n is a weak prime | if n == mean : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT |
Driver code | n = 53 NEW_LINE if isBalancedPrime ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Python3 implementation to count the number of nodes having odd number of divisors for each query | import math NEW_LINE N = 100001 NEW_LINE |
Adjacency list for tree . | adj = [ [ ] for i in range ( N ) ] NEW_LINE |
Array for values and answer at ith node . | a = [ 0 for i in range ( N ) ] NEW_LINE ans = [ 0 for i in range ( N ) ] NEW_LINE |
Function to check whether N has odd divisors or not | def hasOddNumberOfDivisors ( n ) : NEW_LINE INDENT if ( math . sqrt ( n ) == int ( math . sqrt ( n ) ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT |
DFS function to pre - compute the answers | def dfs ( node , parent ) : NEW_LINE |
Initialize the count | count = 0 NEW_LINE for i in adj [ node ] : NEW_LINE INDENT if ( i != parent ) : NEW_LINE DEDENT |
Repeat for every child | count += dfs ( i , node ) NEW_LINE |
Increase the count if current node has odd number of divisors | if ( hasOddNumberOfDivisors ( a [ node ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT ans [ node ] = count NEW_LINE return count NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE i = 0 NEW_LINE q = [ 4 , 1 , 5 , 3 ] NEW_LINE DEDENT |
Adjacency List | adj [ 1 ] . append ( 2 ) NEW_LINE adj [ 2 ] . append ( 1 ) NEW_LINE adj [ 2 ] . append ( 3 ) NEW_LINE adj [ 3 ] . append ( 2 ) NEW_LINE adj [ 3 ] . append ( 4 ) NEW_LINE adj [ 4 ] . append ( 3 ) NEW_LINE adj [ 1 ] . append ( 5 ) NEW_LINE adj [ 5 ] . append ( 1 ) NEW_LINE a [ 1 ] = 4 NEW_LINE a [ 2 ] = 9 NEW_LINE a [ 3 ] = 14 NEW_LINE a [ 4 ] = 100 NEW_LINE a [ 5 ] = 5 NEW_LINE |
Function call | dfs ( 1 , - 1 ) NEW_LINE for i in range ( len ( q ) ) : NEW_LINE INDENT print ( ans [ q [ i ] ] , end = ' ▁ ' ) NEW_LINE DEDENT |
Python3 implementation to find the minimum cost to make all array elements equal | def lowerBound ( array , length , value ) : NEW_LINE INDENT low = 0 NEW_LINE high = length NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE DEDENT DEDENT |
Checks if the value is less than middle element of the array | if ( value <= array [ mid ] ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT return low NEW_LINE |
Function that returns the cost of making all elements equal to current element | def costCalculation ( current , arr , n , pref , a , r , minimum ) : NEW_LINE |
Compute the lower bound of current element | index = lowerBound ( arr , len ( arr ) , current ) NEW_LINE |
Calculate the requirement of add operation | left = index * current - pref [ index ] NEW_LINE |
Calculate the requirement of subtract operation | right = ( pref [ n ] - pref [ index ] - ( n - index ) * current ) NEW_LINE |
Compute minimum of left and right | res = min ( left , right ) NEW_LINE left -= res NEW_LINE right -= res NEW_LINE |
Computing the total cost of add and subtract operations | total = res * minimum NEW_LINE total += left * a NEW_LINE total += right * r NEW_LINE return total NEW_LINE |
Function that prints minimum cost of making all elements equal | def solve ( arr , n , a , r , m ) : NEW_LINE |
Sort the given array | arr . sort ( ) NEW_LINE |
Calculate minimum from a + r and m | minimum = min ( a + r , m ) NEW_LINE pref = [ 0 ] * ( n + 1 ) NEW_LINE |
Compute prefix sum and store in pref array | for i in range ( n ) : NEW_LINE INDENT pref [ i + 1 ] = pref [ i ] + arr [ i ] NEW_LINE DEDENT ans = 10000 NEW_LINE |
Find the minimum cost from the given elements | for i in range ( n ) : NEW_LINE INDENT ans = min ( ans , costCalculation ( arr [ i ] , arr , n , pref , a , r , minimum ) ) NEW_LINE DEDENT |
Finding the minimum cost from the other cases where minimum cost can occur | ans = min ( ans , costCalculation ( pref [ n ] // n , arr , n , pref , a , r , minimum ) ) NEW_LINE ans = min ( ans , costCalculation ( pref [ n ] // n + 1 , arr , n , pref , a , r , minimum ) ) NEW_LINE |
Printing the minimum cost of making all elements equal | print ( ans ) NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 5 , 3 , 6 , 5 ] NEW_LINE A = 1 NEW_LINE R = 2 NEW_LINE M = 4 NEW_LINE size = len ( arr ) NEW_LINE DEDENT |
Function call | solve ( arr , size , A , R , M ) NEW_LINE |
Python3 program to count the number of integers upto N which are of the form of binary representations | from math import * NEW_LINE |
Function to return the count | def countBinaries ( N ) : NEW_LINE INDENT ctr = 1 NEW_LINE ans = 0 NEW_LINE while ( N > 0 ) : NEW_LINE DEDENT |
If the current last digit is 1 | if ( N % 10 == 1 ) : NEW_LINE |
Add 2 ^ ( ctr - 1 ) possible integers to the answer | ans += pow ( 2 , ctr - 1 ) NEW_LINE |
If the current digit exceeds 1 | elif ( N % 10 > 1 ) : NEW_LINE |
Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained | ans = pow ( 2 , ctr ) - 1 NEW_LINE ctr += 1 NEW_LINE N //= 10 NEW_LINE return ans NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 20 NEW_LINE print ( int ( countBinaries ( N ) ) ) NEW_LINE DEDENT |
Function to return the count | def countBinaries ( N ) : NEW_LINE |
PreCompute and store the powers of 2 | powersOfTwo = [ 0 ] * 11 NEW_LINE powersOfTwo [ 0 ] = 1 NEW_LINE for i in range ( 1 , 11 ) : NEW_LINE INDENT powersOfTwo [ i ] = powersOfTwo [ i - 1 ] * 2 NEW_LINE DEDENT ctr = 1 NEW_LINE ans = 0 NEW_LINE while ( N > 0 ) : NEW_LINE |
If the current last digit is 1 | if ( N % 10 == 1 ) : NEW_LINE |
Add 2 ^ ( ctr - 1 ) possible integers to the answer | ans += powersOfTwo [ ctr - 1 ] NEW_LINE |
If the current digit exceeds 1 | elif ( N % 10 > 1 ) : NEW_LINE |
Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained | ans = powersOfTwo [ ctr ] - 1 NEW_LINE ctr += 1 NEW_LINE N = N // 10 NEW_LINE return ans NEW_LINE |
Driver code | N = 20 NEW_LINE print ( countBinaries ( N ) ) NEW_LINE |
Centered_Hexadecagonal number function | def Centered_Hexadecagonal_num ( n ) : NEW_LINE |
Formula to calculate nth Centered_Hexadecagonal number & return it into main function . | return ( 8 * n * n - 8 * n + 1 ) NEW_LINE |
Function to find the sum of the first N Centered Hexadecagonal number | def sum_Centered_Hexadecagonal_num ( n ) : NEW_LINE |
Variable to store the sum | summ = 0 NEW_LINE |
Loop to iterate through the first N numbers | for i in range ( 1 , n + 1 ) : NEW_LINE |
Find the sum | summ += Centered_Hexadecagonal_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_Hexadecagonal number | print ( sum_Centered_Hexadecagonal_num ( n ) ) NEW_LINE |
Function to find N - th centered heptagonal number | def center_heptagonal_num ( n ) : NEW_LINE |
Formula to calculate nth centered heptagonal number | return ( 7 * n * n - 7 * n + 2 ) // 2 NEW_LINE |
Function to find the sum of the first N centered heptagonal numbers | def sum_center_heptagonal_num ( n ) : NEW_LINE |
Variable to store the sum | summ = 0 NEW_LINE |
Iterate through the range 1 to N | for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += center_heptagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_center_heptagonal_num ( n ) ) NEW_LINE DEDENT |
Function to find the N - th Centered Dodecagonal number | def Centered_Dodecagonal_num ( n ) : NEW_LINE |
Formula to calculate nth Centered_Dodecagonal number | return 6 * n * ( n - 1 ) + 1 NEW_LINE |
Function to find the sum of the first N Centered_Dodecagonal number | def sum_Centered_Dodecagonal_num ( n ) : NEW_LINE |
Variable to store the sum | summ = 0 NEW_LINE |
Iterating from 1 to N | for i in range ( 1 , n + 1 ) : NEW_LINE |
Finding the sum | summ += Centered_Dodecagonal_num ( i ) NEW_LINE return summ NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_Dodecagonal_num ( n ) ) NEW_LINE DEDENT |
Function to find N - th Centered Octagonal number | def center_Octagonal_num ( n ) : NEW_LINE |
Formula to calculate nth centered Octagonal number | return ( 4 * n * n - 4 * n + 1 ) NEW_LINE |
Function to find the sum of the first N Centered Octagonal numbers | def sum_center_Octagonal_num ( n ) : NEW_LINE |
Variable to store the sum | summ = 0 NEW_LINE |
Iterating through the first N numbers | for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += center_Octagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_center_Octagonal_num ( n ) ) NEW_LINE DEDENT |
Function to find the N - th centred decagonal number | def Centered_decagonal_num ( n ) : NEW_LINE |
Formula to calculate nth Centered_decagonal number & return it into main function . | return ( 5 * n * n - 5 * n + 1 ) NEW_LINE |
Function to find the sum of the first N Centered decagonal numbers | def sum_Centered_decagonal_num ( n ) : NEW_LINE |
Variable to store the sum | summ = 0 NEW_LINE |
Iterating through the range | for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_decagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE DEDENT |
display first Nth Centered_decagonal number | print ( sum_Centered_decagonal_num ( n ) ) NEW_LINE |
Function to find the N - th Centered octadecagonal number | def center_octadecagon_num ( n ) : NEW_LINE |
Formula to calculate nth centered octadecagonal number | return ( 9 * n * n - 9 * n + 1 ) NEW_LINE |
Function to find the sum of the first N Centered octadecagonal numbers | def sum_center_octadecagon_num ( n ) : NEW_LINE |
Variable to store the sum | summ = 0 NEW_LINE |
Iterating through the range 1 to N | for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += center_octadecagon_num ( i ) NEW_LINE DEDENT return summ NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( sum_center_octadecagon_num ( n ) ) NEW_LINE DEDENT |
Function to find the Centered_Pentadecagonal number | def Centered_Pentadecagonal_num ( n ) : NEW_LINE |
Formula to calculate N - th Centered_Pentadecagonal number | return ( 15 * n * n - 15 * n + 2 ) // 2 NEW_LINE |
Function to find the sum of the first N Centered_Pentadecagonal numbers | def sum_Centered_Pentadecagonal_num ( n ) : NEW_LINE |
Variable to store the sum | summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_Pentadecagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_Pentadecagonal_num ( n ) ) NEW_LINE DEDENT |
Python3 program for the above approach | from math import sqrt NEW_LINE |
Function to check if N is a octagonal number | def isoctagonal ( N ) : NEW_LINE INDENT n = ( 2 + sqrt ( 12 * N + 4 ) ) / 6 ; NEW_LINE DEDENT |
Condition to check if the number is a octagonal number | return ( n - int ( n ) ) == 0 ; NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given number | N = 8 ; NEW_LINE |
Function call | if ( isoctagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT |
Python3 program for the above approach | from math import sqrt NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.