text
stringlengths
1
636
code
stringlengths
8
1.89k
N must be odd at this point . So we can skip one element
for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE
While i divides n , print i and divide n
while ( n % i == 0 ) : NEW_LINE INDENT if ( ( int ( N / i ) - 1 ) % i != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = int ( n / i ) NEW_LINE DEDENT
This condition is to handle the case when n is a prime number > 2
if ( n > 2 ) : NEW_LINE INDENT if ( ( int ( N / n ) - 1 ) % n != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE
Given Number N
n = 30 NEW_LINE
Function Call
if ( isGiugaNum ( 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 droll numbers
def isDroll ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT
To store sum of even prime factors
sum_even = 0 ; NEW_LINE
To store sum of odd prime factors
sum_odd = 0 ; NEW_LINE
Add the number of 2 s that divide n in sum_even
while ( n % 2 == 0 ) : NEW_LINE INDENT sum_even += 2 ; NEW_LINE n = n // 2 ; NEW_LINE DEDENT
N must be odd at this point . So we can skip one element ( Note i = i + 2 )
for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE
While i divides n , print i and divide n
while ( n % i == 0 ) : NEW_LINE INDENT sum_odd += i ; NEW_LINE n = n // i ; NEW_LINE DEDENT
This condition is to handle the case when n is a prime number greater than 2
if ( n > 2 ) : NEW_LINE INDENT sum_odd += n ; NEW_LINE DEDENT
Condition to check droll number
return sum_even == sum_odd ; NEW_LINE
Given Number N
N = 72 ; NEW_LINE
Function Call
if ( isDroll ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Python3 program to count all pairs of divisors such that their sum is coprime with N
import math as m NEW_LINE
Function to count all valid pairs
def CountPairs ( n ) : NEW_LINE
initialize count
cnt = 0 NEW_LINE i = 1 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT div1 = i NEW_LINE div2 = n // i NEW_LINE sum = div1 + div2 ; NEW_LINE DEDENT DEDENT
Check if sum of pair and n are coprime
if ( m . gcd ( sum , n ) == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT i += 1 NEW_LINE
Return the result
return cnt NEW_LINE
Driver code
n = 24 NEW_LINE print ( CountPairs ( n ) ) NEW_LINE
Function to find if it is possible to make A equal to B
def isPossible ( A , B ) : NEW_LINE INDENT return ( A - B > 1 ) ; NEW_LINE DEDENT
Driver Code
A = 10 ; B = 4 ; NEW_LINE
Function Call
if ( isPossible ( A , B ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Python3 program to maximize the sum of minimum difference of divisors of nodes in an n - ary tree
import math NEW_LINE
Array to store the result at each node
sub = [ 0 for i in range ( 100005 ) ] NEW_LINE
Function to get minimum difference between the divisors of a number
def minDivisorDifference ( n ) : NEW_LINE INDENT num1 = 0 NEW_LINE num2 = 0 NEW_LINE DEDENT
Iterate from square root of N to N
for i in range ( int ( math . sqrt ( n ) ) , n + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT num1 = i NEW_LINE num2 = n // i NEW_LINE break NEW_LINE DEDENT DEDENT
Return absolute difference
return abs ( num1 - num2 ) NEW_LINE
DFS function to calculate the maximum sum
def dfs ( g , u , par ) : NEW_LINE
Store the min difference
sub [ u ] = minDivisorDifference ( u ) NEW_LINE mx = 0 NEW_LINE for c in g [ u ] : NEW_LINE INDENT if ( c != par ) : NEW_LINE INDENT ans = dfs ( g , c , u ) NEW_LINE mx = max ( mx , ans ) NEW_LINE DEDENT DEDENT
Add the maximum of all children to sub [ u ]
sub [ u ] += mx NEW_LINE
Return maximum sum of node ' u ' to its parent
return sub [ u ] NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT g = [ [ ] for i in range ( 100005 ) ] NEW_LINE edges = 6 NEW_LINE g [ 18 ] . append ( 7 ) NEW_LINE g [ 7 ] . append ( 18 ) NEW_LINE g [ 18 ] . append ( 15 ) NEW_LINE g [ 15 ] . append ( 18 ) NEW_LINE g [ 15 ] . append ( 2 ) NEW_LINE g [ 2 ] . append ( 15 ) NEW_LINE g [ 7 ] . append ( 4 ) NEW_LINE g [ 4 ] . append ( 7 ) NEW_LINE g [ 7 ] . append ( 12 ) NEW_LINE g [ 12 ] . append ( 7 ) NEW_LINE g [ 12 ] . append ( 9 ) NEW_LINE g [ 9 ] . append ( 12 ) NEW_LINE root = 18 NEW_LINE print ( dfs ( g , root , - 1 ) ) NEW_LINE DEDENT
Function to check if N is a centered cubic number
def isCenteredcube ( N ) : NEW_LINE
Iterating from 1
i = 1 ; NEW_LINE
Infinite loop
while ( True ) : NEW_LINE
Finding ith_term
ith_term = ( ( 2 * i + 1 ) * ( i * i + i + 1 ) ) ; NEW_LINE
Checking if the number N is a centered cube number
if ( ith_term == N ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT
If ith_term > N then N is not a centered cube number
if ( ith_term > N ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT
Incrementing i
i += 1 ; NEW_LINE
Driver code
N = 9 ; NEW_LINE
Function call
if ( isCenteredcube ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Function to calculate product of geometric series
def productOfGP ( a , r , n ) : NEW_LINE
Initialise final product with 1
product = 1 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE
Multiply product with each term stored in a
product = product * a ; NEW_LINE a = a * r ; NEW_LINE
Return the final product
return product ; NEW_LINE
Given first term and common ratio
a = 1 NEW_LINE r = 2 ; NEW_LINE
Number of terms
N = 4 ; NEW_LINE
Function Call
print ( productOfGP ( a , r , N ) ) NEW_LINE
Function to find GCD of a & b using Euclid Lemma
def gcd ( a , b ) : NEW_LINE
Base Case
if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE
Function to find the LCM of all elements in arr [ ]
def findlcm ( arr , n ) : NEW_LINE
Initialize result
ans = arr [ 0 ] NEW_LINE
Iterate arr [ ] to find LCM
for i in range ( 1 , n ) : NEW_LINE INDENT ans = ( ( ( arr [ i ] * ans ) ) // ( gcd ( arr [ i ] , ans ) ) ) NEW_LINE DEDENT
Return the final LCM
return ans NEW_LINE
Function to find the sum of N fraction in reduced form
def addReduce ( n , num , den ) : NEW_LINE
To store the sum of all final numerators
final_numerator = 0 NEW_LINE
Find the LCM of all denominator
final_denominator = findlcm ( den , n ) NEW_LINE
Find the sum of all N numerators & denominators
for i in range ( n ) : NEW_LINE
Add each fraction one by one
final_numerator = ( final_numerator + ( num [ i ] ) * ( final_denominator // den [ i ] ) ) NEW_LINE
Find GCD of final numerator and denominator
GCD = gcd ( final_numerator , final_denominator ) NEW_LINE
Convert into reduced form by dividing from GCD
final_numerator //= GCD NEW_LINE final_denominator //= GCD NEW_LINE
Print the final fraction
print ( final_numerator , " / " , final_denominator ) NEW_LINE
Given N
N = 3 NEW_LINE
Given Numerator
arr1 = [ 1 , 2 , 5 ] NEW_LINE
Given Denominator
arr2 = [ 2 , 1 , 6 ] NEW_LINE
Function call
addReduce ( N , arr1 , arr2 ) NEW_LINE
Python3 program to find minimum possible lcm from any pair
import sys NEW_LINE
Function to compute GCD of two numbers
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT return gcd ( b , a % b ) ; NEW_LINE DEDENT
Function that return minimum possible lcm from any pair
def minLCM ( arr , n ) : NEW_LINE INDENT ans = 1000000000 ; NEW_LINE for i in range ( n ) : NEW_LINE DEDENT
Fix the ith element and iterate over all the array to find minimum LCM
for j in range ( i + 1 , n ) : NEW_LINE INDENT g = gcd ( arr [ i ] , arr [ j ] ) ; NEW_LINE lcm = arr [ i ] / g * arr [ j ] ; NEW_LINE ans = min ( ans , lcm ) ; NEW_LINE DEDENT return ans ; NEW_LINE
Driver code
arr = [ 2 , 4 , 3 , 6 , 5 ] ; NEW_LINE print ( minLCM ( arr , 5 ) ) NEW_LINE
Python3 implementation to find the values of x and y for the given equation with integer N
from math import pow , ceil NEW_LINE
Function which find required x & y
def solve ( n ) : NEW_LINE
Upper limit of x & y , if such x & y exists
upper_limit = ceil ( pow ( n , 1.0 / 4 ) ) ; NEW_LINE for x in range ( upper_limit + 1 ) : NEW_LINE INDENT for y in range ( upper_limit + 1 ) : NEW_LINE DEDENT
num1 stores x ^ 4
num1 = x * x * x * x ; NEW_LINE
num2 stores y ^ 4
num2 = y * y * y * y ; NEW_LINE
If condition is satisfied the print and return
if ( num1 - num2 == n ) : NEW_LINE INDENT print ( " x ▁ = " , x , " , ▁ y ▁ = " , y ) ; NEW_LINE return ; NEW_LINE DEDENT
If no such pair exists
print ( - 1 ) ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 15 ; NEW_LINE solve ( n ) ; NEW_LINE DEDENT
Python3 program for the above approach
import math NEW_LINE
Function to check if count of even and odd divisors are equal
def divisorsSame ( n ) : NEW_LINE
To store the count of even factors and odd factors
even_div = 0 ; odd_div = 0 ; NEW_LINE
Loop till [ 1 , sqrt ( N ) ]
for i in range ( 1 , int ( math . sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE DEDENT
If divisors are equal add only one
if ( n // i == i ) : NEW_LINE
Check for even divisor
if ( i % 2 == 0 ) : NEW_LINE INDENT even_div += 1 ; NEW_LINE DEDENT
Odd divisor
else : NEW_LINE INDENT odd_div += 1 ; NEW_LINE DEDENT
Check for both divisor i . e . , i and N / i
else : NEW_LINE
Check if i is odd or even
if ( i % 2 == 0 ) : NEW_LINE INDENT even_div += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT odd_div += 1 ; NEW_LINE DEDENT
Check if N / i is odd or even
if ( n // ( i % 2 ) == 0 ) : NEW_LINE INDENT even_div += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT odd_div += 1 ; NEW_LINE DEDENT
Return true if count of even_div and odd_div are equals
return ( even_div == odd_div ) ; NEW_LINE
Given Number
N = 6 ; NEW_LINE
Function Call
if ( divisorsSame ( N ) == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Utility function to check if a number is prime or not
def isPrime ( n ) : NEW_LINE
Corner cases
if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n <= 3 : NEW_LINE INDENT return True NEW_LINE DEDENT
This is checked so that we can skip middle five numbers in below loop
if n % 2 == 0 or n % 3 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return True NEW_LINE
Function that returns true if n is a Balanced prime
def isBalancedPrime ( n ) : NEW_LINE
If n is not a prime number or n is the first prime then return false
if not isPrime ( n ) or n == 2 : NEW_LINE INDENT return False NEW_LINE DEDENT