text
stringlengths
1
636
code
stringlengths
8
1.89k
No distribution possible
if ( N < 4 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
Total number of ways to distribute N items among 3 people
ans = ( ( N - 1 ) * ( N - 2 ) ) // 2 NEW_LINE
Store the number of distributions which are not possible
s = 0 NEW_LINE for i in range ( 2 , N - 2 , 1 ) : NEW_LINE INDENT for j in range ( 1 , i , 1 ) : NEW_LINE DEDENT
Count possibilities of two persons receiving the maximum
if ( N == 2 * i + j ) : NEW_LINE INDENT s += 1 NEW_LINE DEDENT
If N is divisible by 3
if ( N % 3 == 0 ) : NEW_LINE INDENT s = 3 * s + 1 NEW_LINE DEDENT else : NEW_LINE INDENT s = 3 * s NEW_LINE DEDENT
Return the final count of ways to distribute
return ans - s NEW_LINE
Driver Code
N = 10 NEW_LINE print ( countWays ( N ) ) NEW_LINE
Function to check if n is prime
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 = i + 6 NEW_LINE DEDENT return True NEW_LINE
Function to check if the number is Magnanimous or not
def isMagnanimous ( N ) : NEW_LINE
Converting the number to string
s = str ( N ) NEW_LINE
Finding length of string
l = len ( s ) NEW_LINE
Number should not be of single digit
if ( l < 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT
Loop to find all left and right part of the string
for i in range ( l - 1 ) : NEW_LINE INDENT left = s [ 0 : i + 1 ] NEW_LINE right = s [ i + 1 : ] NEW_LINE x = int ( left ) NEW_LINE y = int ( right ) NEW_LINE if ( not isPrime ( x + y ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE
Driver code
N = 12 NEW_LINE if isMagnanimous ( N ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Python3 program for the above approach
limit = 10000000 NEW_LINE position = [ 0 ] * ( limit + 1 ) NEW_LINE
Function to precompute the position of every prime number using Sieve
def sieve ( ) : NEW_LINE
0 and 1 are not prime numbers
position [ 0 ] = - 1 NEW_LINE position [ 1 ] = - 1 NEW_LINE
Variable to store the position
pos = 0 NEW_LINE for i in range ( 2 , limit + 1 ) : NEW_LINE INDENT if ( position [ i ] == 0 ) : NEW_LINE DEDENT
Incrementing the position for every prime number
pos += 1 NEW_LINE position [ i ] = pos NEW_LINE for j in range ( i * 2 , limit + 1 , i ) : NEW_LINE INDENT position [ j ] = - 1 NEW_LINE DEDENT
Function to get sum of digits
def getSum ( n ) : NEW_LINE INDENT Sum = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT Sum = Sum + n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT return Sum NEW_LINE DEDENT
Function to check whether the given number is Honaker Prime number or not
def isHonakerPrime ( n ) : NEW_LINE INDENT pos = position [ n ] NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return bool ( getSum ( n ) == getSum ( pos ) ) NEW_LINE DEDENT
Precompute the prime numbers till 10 ^ 6
sieve ( ) NEW_LINE
Given Number
N = 121 NEW_LINE
Function Call
if ( isHonakerPrime ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Python3 implementation to check if the sum of matrix is prime or not
import math NEW_LINE
Function to check whether a number is prime or not
def isPrime ( n ) : NEW_LINE
Corner case
if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT
Check from 2 to n - 1
for i in range ( 2 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE
Function for to find the sum of the given matrix
def takeSum ( a ) : NEW_LINE INDENT s = 0 NEW_LINE for i in range ( 0 , 4 ) : NEW_LINE INDENT for j in range ( 0 , 5 ) : NEW_LINE INDENT s += a [ i ] [ j ] NEW_LINE DEDENT DEDENT return s ; NEW_LINE DEDENT
Driver Code
a = [ [ 1 , 2 , 3 , 4 , 2 ] , [ 0 , 1 , 2 , 3 , 34 ] , [ 0 , 34 , 21 , 12 , 12 ] , [ 1 , 2 , 3 , 6 , 6 ] ] ; NEW_LINE sum = takeSum ( a ) ; NEW_LINE if ( isPrime ( sum ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Function to find the sum
def sumOfSumSeries ( N ) : NEW_LINE INDENT _sum = 0 NEW_LINE DEDENT
Calculate sum - series for every natural number and add them
for i in range ( N + 1 ) : NEW_LINE INDENT _sum = _sum + ( i * ( i + 1 ) ) // 2 NEW_LINE DEDENT return _sum NEW_LINE
Driver code
N = 5 NEW_LINE print ( sumOfSumSeries ( N ) ) NEW_LINE
Function to find the sum
def sumOfSumSeries ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) * ( n + 2 ) ) // 6 NEW_LINE DEDENT
Driver code
N = 5 NEW_LINE print ( sumOfSumSeries ( N ) ) NEW_LINE
Function to check if the number N having all digits lies in the set ( 0 , 1 , 8 )
def isContaindigit ( n ) : NEW_LINE INDENT temp = str ( n ) NEW_LINE for i in temp : NEW_LINE INDENT if i not in [ '0' , '1' , '8' ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT
Function to check if the number N is palindrome
def ispalindrome ( n ) : NEW_LINE INDENT temp = str ( n ) NEW_LINE if temp == temp [ : : - 1 ] : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT
Function to check if a number N is Tetradic
def isTetradic ( n ) : NEW_LINE INDENT if ispalindrome ( n ) : NEW_LINE INDENT if isContaindigit ( n ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT
Function to generate all primes and checking whether number is Tetradic or not
def printTetradicPrimesLessThanN ( n ) : NEW_LINE
Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true .
prime = [ True ] * ( n + 1 ) ; NEW_LINE p = 2 ; NEW_LINE while ( p * p <= n ) : NEW_LINE
If prime [ p ] is not changed , then it is a prime
if ( prime [ p ] ) : NEW_LINE
Update all multiples of p
for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT p += 1 ; NEW_LINE
Print all Tetradic prime numbers
for p in range ( 2 , n + 1 ) : NEW_LINE
checking whether the given number is prime Tetradic or not
if ( prime [ p ] and isTetradic ( p ) ) : NEW_LINE INDENT print ( p , end = " ▁ " ) ; NEW_LINE DEDENT
Driver Code
n = 1000 ; NEW_LINE printTetradicPrimesLessThanN ( n ) ; NEW_LINE
Function to concatenate two integers into one
def concat ( a , b ) : NEW_LINE
Convert both the integers to string
s1 = str ( a ) NEW_LINE s2 = str ( b ) NEW_LINE
Concatenate both strings
s = s1 + s2 NEW_LINE
Convert the concatenated string to integer
c = int ( s ) NEW_LINE
return the formed integer
return c NEW_LINE
Function to check if N is a Astonishing number
def isAstonishing ( n ) : NEW_LINE
Loop to find sum of all integers from i till the sum becomes >= n
for i in range ( n ) : NEW_LINE
variable to store sum of all integers from i to j and check if sum and concatenation equals n or not
sum = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum += j NEW_LINE if ( sum == n ) : NEW_LINE DEDENT
finding concatenation of i and j
concatenation = concat ( i , j ) NEW_LINE
condition for Astonishing number
if ( concatenation == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE
Given Number
n = 429 NEW_LINE
Function Call
if ( isAstonishing ( n ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT
Function to check if the digits in the number is the same number of digits
def checkSame ( n , b ) : NEW_LINE INDENT m = { } NEW_LINE DEDENT
Loop to iterate over the digits of the number N
while ( n != 0 ) : NEW_LINE INDENT r = n % b NEW_LINE n = n // b NEW_LINE if r in m : NEW_LINE INDENT m [ r ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ r ] = 1 NEW_LINE DEDENT DEDENT last = - 1 NEW_LINE
Loop to iterate over the map
for i in m : NEW_LINE INDENT if last != - 1 and m [ i ] != last : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT last = m [ i ] NEW_LINE DEDENT DEDENT return True NEW_LINE
Driver code
n = 9 NEW_LINE base = 2 NEW_LINE
Function to check
if ( checkSame ( n , base ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Function to calculate the sum upto Nth term
def seriesSum ( n ) : NEW_LINE
Stores the sum of the series
sum1 = 0 ; NEW_LINE
Stores the product of natural numbers upto the current term
currProd = 1 ; NEW_LINE
Stores the sum of natural numbers upto the upto current term
currSum = 1 ; NEW_LINE
Generate the remaining terms and calculate sum
for i in range ( 2 , n + 1 ) : NEW_LINE INDENT currProd *= i ; NEW_LINE currSum += i ; NEW_LINE DEDENT
Update the sum
sum1 += currProd - currSum ; NEW_LINE
Return the sum
return sum1 ; NEW_LINE
Driver Code
N = 5 ; NEW_LINE print ( seriesSum ( N ) , end = " ▁ " ) ; NEW_LINE
Function to count the number of elements of array which are not divisible by any other element in the array arr [ ]
def count ( a , n ) : NEW_LINE INDENT countElements = 0 NEW_LINE DEDENT
Iterate over the array
for i in range ( n ) : NEW_LINE INDENT flag = True NEW_LINE for j in range ( n ) : NEW_LINE DEDENT
Check if the element is itself or not
if ( i == j ) : NEW_LINE INDENT continue NEW_LINE DEDENT
Check for divisibility
if ( a [ i ] % a [ j ] == 0 ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT if ( flag == True ) : NEW_LINE countElements += 1 NEW_LINE
Return the final result
return countElements NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Given array
arr = [ 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE
Function Call
print ( count ( arr , n ) ) NEW_LINE
Python3 program for the above approach
import math NEW_LINE
Function to find the smallest N - digit number divisible by N
def smallestNumber ( N ) : NEW_LINE
Return the smallest N - digit number calculated using above formula
return N * math . ceil ( pow ( 10 , ( N - 1 ) ) // N ) ; NEW_LINE
Given N
N = 2 ; NEW_LINE
Function Call
print ( smallestNumber ( N ) ) ; NEW_LINE
Function to count the pairs in the array such as there is at least one even element in each pair
def CountPairs ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT
Generate all possible pairs and increment then count if the condition is satisfied
for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 or arr [ j ] % 2 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE
Driver code
arr = [ 8 , 2 , 3 , 1 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE
Function call
print ( CountPairs ( arr , n ) ) NEW_LINE
Function to count the pairs in the array such as there is at least one even element in each pair
def CountPairs ( arr , n ) : NEW_LINE
Store count of even and odd elements
even = 0 NEW_LINE odd = 0 NEW_LINE for i in range ( n ) : NEW_LINE
Check element is even or odd
if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT return ( ( even * ( even - 1 ) ) // 2 + ( even * odd ) ) NEW_LINE
Driver Code
arr = [ 8 , 2 , 3 , 1 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( CountPairs ( arr , n ) ) NEW_LINE
Python program for the above approach
import math NEW_LINE
Function to check if n is a composite number
def isComposite ( n ) : NEW_LINE
Corner cases
if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT
This is checked to skip middle 5 numbers
if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return True 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 True NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return False NEW_LINE
Function to check if N is a Giuga Number
def isGiugaNum ( n ) : NEW_LINE
N should be composite to be a Giuga Number
if ( not ( isComposite ( n ) ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT N = n NEW_LINE
Print the number of 2 s that divide n
while ( n % 2 == 0 ) : NEW_LINE INDENT if ( ( int ( N / 2 ) - 1 ) % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = int ( n / 2 ) NEW_LINE DEDENT