codeparrot/xlcost-text-to-code huggingface dataset:
Find the super power of a given Number | Python3 for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver Code
MAX = 100000 ; NEW_LINE prime = [ True ] * 100002 ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT p = 2 ; NEW_LINE while ( p * p <= MAX ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT i = p * 2 ; NEW_LINE while ( i <= MAX ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE i += p ; NEW_LINE DEDENT DEDENT p += 1 ; NEW_LINE DEDENT DEDENT def superpower ( n ) : NEW_LINE INDENT SieveOfEratosthenes ( ) ; NEW_LINE superPower = 0 ; NEW_LINE factor = 0 ; NEW_LINE i = 2 ; NEW_LINE while ( n > 1 and i <= MAX ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT factor = 0 ; NEW_LINE while ( n % i == 0 and n > 1 ) : NEW_LINE INDENT factor += 1 ; NEW_LINE n = int ( n / i ) ; NEW_LINE DEDENT if ( superPower < factor ) : NEW_LINE INDENT superPower = factor ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT return superPower ; NEW_LINE DEDENT n = 256 ; NEW_LINE print ( superpower ( n ) ) ; NEW_LINE
Count of Prime Nodes of a Singly Linked List | Function to check if a number is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Link list node ; Push a new node on the front of the list . ; Function to find count of prime nodes in a linked list ; If current node is prime ; Update count ; Driver Code ; Start with the empty list ; create the linked list 15 -> 5 -> 6 -> 10 -> 17 ; Function call to print require answer
def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n <= 3 : NEW_LINE INDENT return True NEW_LINE DEDENT 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 DEDENT class Node : NEW_LINE INDENT def __init__ ( self , data , next ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( new_data , self . head ) NEW_LINE self . head = new_node NEW_LINE DEDENT def countPrime ( self ) : NEW_LINE INDENT count = 0 NEW_LINE ptr = self . head NEW_LINE while ptr != None : NEW_LINE INDENT if isPrime ( ptr . data ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT ptr = ptr . next NEW_LINE DEDENT return count NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT linkedlist = LinkedList ( ) NEW_LINE linkedlist . push ( 17 ) NEW_LINE linkedlist . push ( 10 ) NEW_LINE linkedlist . push ( 6 ) NEW_LINE linkedlist . push ( 5 ) NEW_LINE linkedlist . push ( 15 ) NEW_LINE print ( " Count ▁ of ▁ prime ▁ nodes ▁ = " , linkedlist . countPrime ( ) ) NEW_LINE DEDENT
Smallest prime divisor of a number | Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code
def smallestDivisor ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT i = 3 ; NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT i += 2 ; NEW_LINE DEDENT return n ; NEW_LINE DEDENT n = 31 ; NEW_LINE print ( smallestDivisor ( n ) ) ; NEW_LINE
Count Number of animals in a zoo from given number of head and legs | Function that calculates Rabbits ; Driver code
def countRabbits ( Heads , Legs ) : NEW_LINE INDENT count = 0 NEW_LINE count = ( Legs ) - 2 * ( Heads ) NEW_LINE count = count / 2 NEW_LINE return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Heads = 100 NEW_LINE Legs = 300 NEW_LINE Rabbits = countRabbits ( Heads , Legs ) NEW_LINE print ( " Rabbits ▁ = " , Rabbits ) NEW_LINE print ( " Pigeons ▁ = " , Heads - Rabbits ) NEW_LINE DEDENT
Program to evaluate the expression ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X + 1 ) ^ 6 + ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X | ''Python3 program to evaluate the given expression ; ''Function to find the sum ; ''Driver Code
import math NEW_LINE def calculateSum ( n ) : NEW_LINE INDENT a = int ( n ) NEW_LINE return ( 2 * ( pow ( n , 6 ) + 15 * pow ( n , 4 ) + 15 * pow ( n , 2 ) + 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1.4142 NEW_LINE DEDENT print ( math . ceil ( calculateSum ( n ) ) ) NEW_LINE
Find the sum of series 3 , | calculate sum upto N term of series ; Driver code
def Sum_upto_nth_Term ( n ) : NEW_LINE INDENT return ( 1 - pow ( - 2 , n ) ) NEW_LINE DEDENT N = 5 NEW_LINE print ( Sum_upto_nth_Term ( N ) ) NEW_LINE
Count numbers whose XOR with N is equal to OR with N | Function to calculate count of numbers with XOR equals OR ; variable to store count of unset bits ; Driver code
def xorEqualsOrCount ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT bit = N % 2 NEW_LINE if bit == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT N //= 2 NEW_LINE DEDENT return int ( pow ( 2 , count ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE print ( xorEqualsOrCount ( N ) ) NEW_LINE DEDENT
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | Method to find the factorial of a number ; Method to compute the sum ; Iterate the loop till n and compute the formula ; Driver code ; Get x and n ; Print output
def fact ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return n * fact ( n - 1 ) NEW_LINE DEDENT DEDENT def sum ( x , n ) : NEW_LINE INDENT total = 1.0 NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT total = total + ( pow ( x , i ) / fact ( i + 1 ) ) NEW_LINE DEDENT return total NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 5 NEW_LINE n = 4 NEW_LINE print ( " Sum ▁ is : ▁ { 0 : . 4f } " . format ( sum ( x , n ) ) ) NEW_LINE DEDENT
Find Sum of Series 1 ^ 2 | Function to find sum of series ; If i is even ; If i is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum
def sum_of_series ( n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT result = result - pow ( i , 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT result = result + pow ( i , 2 ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE n = 10 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE DEDENT
Program to find the sum of the series 23 + 45 + 75 + ... . . upto N terms | calculate Nth term of series ; Driver Function ; Get the value of N ; Get the sum of the series
def findSum ( N ) : NEW_LINE INDENT return ( 2 * N * ( N + 1 ) * ( 4 * N + 17 ) + 54 * N ) / 6 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( findSum ( N ) ) NEW_LINE DEDENT
Program to find the value of sin ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | ''Python3 program to find the value of sin(n-theta)  ; ''Function to calculate the binomial  coefficient upto 15  ; '' use simple DP to find coefficient  ; ''Function to find the value of cos(n-theta) ; '' find sinTheta from sinTheta  ; '' to store required answer  ; '' use to toggle sign in sequence.  ; ''Driver code
import math NEW_LINE MAX = 16 NEW_LINE nCr = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE def binomial ( ) : NEW_LINE for i in range ( MAX ) : for j in range ( 0 , i + 1 ) : if j == 0 or j == i : NEW_LINE INDENT nCr [ i ] [ j ] = 1 NEW_LINE DEDENT else : nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] NEW_LINE def findCosNTheta ( sinTheta , n ) : NEW_LINE cosTheta = math . sqrt ( 1 - sinTheta * sinTheta ) NEW_LINE ans = 0 NEW_LINE INDENT toggle = 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 , 2 ) : ans = ( ans + nCr [ n ] [ i ] * ( cosTheta ** ( n - i ) ) * ( sinTheta ** i ) * toggle ) toggle = toggle * - 1 NEW_LINE return ans NEW_LINE if __name__ == ' _ _ main _ _ ' : binomial ( ) NEW_LINE INDENT sinTheta = 0.5 NEW_LINE n = 10 NEW_LINE print ( findCosNTheta ( sinTheta , n ) ) NEW_LINE DEDENT
Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | Python program to find N - th term of the series : 9 , 23 , 45 , 75 , 113. . . ; calculate Nth term of series ; Get the value of N ; Find the Nth term and print it
def nthTerm ( N ) : NEW_LINE INDENT return ( ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ) ; NEW_LINE DEDENT n = 4 NEW_LINE print ( nthTerm ( n ) ) NEW_LINE
Program to Find the value of cos ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | ''Python3 program to find the value of cos(n-theta) ; ''Function to calculate the binomial coefficient upto 15 ; '' use simple DP to find coefficient ; ''Function to find the value of cos(n-theta) ; ''find sinTheta from cosTheta ; '' to store the required answer ; '' use to toggle sign in sequence. ; ''Driver code
import math NEW_LINE MAX = 16 NEW_LINE nCr = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE def binomial ( ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE for j in range ( 0 , i + 1 ) : NEW_LINE if j == 0 or j == i : NEW_LINE DEDENT nCr [ i ] [ j ] = 1 NEW_LINE INDENT else : NEW_LINE nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT def findCosnTheta ( cosTheta , n ) : NEW_LINE INDENT sinTheta = math . sqrt ( 1 - cosTheta * cosTheta ) NEW_LINE ans = 0 NEW_LINE DEDENT toggle = 1 NEW_LINE for i in range ( 0 , n + 1 , 2 ) : NEW_LINE INDENT ans = ans + nCr [ n ] [ i ] * ( cosTheta ** ( n - i ) ) * ( sinTheta ** i ) * toggle NEW_LINE toggle = toggle * - 1 NEW_LINE DEDENT return ans NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT binomial ( ) NEW_LINE cosTheta = 0.5 NEW_LINE n = 10 NEW_LINE print ( findCosnTheta ( cosTheta , n ) ) NEW_LINE DEDENT
Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | Function to calculate sum ; Return sum ; driver code
def solve_sum ( n ) : NEW_LINE INDENT return ( pow ( 10 , n + 1 ) * ( 9 * n - 1 ) + 10 ) / pow ( 9 , 3 ) - n * ( n + 1 ) / 18 NEW_LINE DEDENT n = 3 NEW_LINE print ( int ( solve_sum ( n ) ) ) NEW_LINE
Find sum of the series 1 | Function to calculate sum ; when n is odd ; when n is not odd ; Driver code
def solve_sum ( n ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return ( n + 1 ) / 2 NEW_LINE DEDENT return - n / 2 NEW_LINE DEDENT n = 8 NEW_LINE print ( int ( solve_sum ( n ) ) ) NEW_LINE
Check if a number can be expressed as a ^ b | Set 2 | Python 3 Program to check if a number can be expressed as a ^ b ; Driver Code
from math import * NEW_LINE def isPower ( a ) : NEW_LINE INDENT if a == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( a ) ) + 1 ) : NEW_LINE INDENT val = log ( a ) / log ( i ) NEW_LINE if ( round ( ( val - int ( val ) ) , 8 ) < 0.00000001 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 16 NEW_LINE if isPower ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Program to calculate Root Mean Square | Python3 program to calculate Root Mean Square ; Function that Calculate Root Mean Square ; Calculate square ; Calculate Mean ; Calculate Root ; Driver code
import math NEW_LINE def rmsValue ( arr , n ) : NEW_LINE INDENT square = 0 NEW_LINE mean = 0.0 NEW_LINE root = 0.0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT square += ( arr [ i ] ** 2 ) NEW_LINE DEDENT mean = ( square / ( float ) ( n ) ) NEW_LINE root = math . sqrt ( mean ) NEW_LINE return root NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 4 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( rmsValue ( arr , n ) ) NEW_LINE DEDENT
Program to find the quantity after mixture replacement | Function to calculate the Remaining amount . ; calculate Right hand Side ( RHS ) . ; calculate Amount left by multiply it with original value . ; Driver Code
def Mixture ( X , Y , Z ) : NEW_LINE INDENT result = 0.0 NEW_LINE result1 = 0.0 NEW_LINE result1 = ( ( X - Y ) / X ) NEW_LINE result = pow ( result1 , Z ) NEW_LINE result = result * X NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 10 NEW_LINE Y = 2 NEW_LINE Z = 2 NEW_LINE print ( " { : . 1f } " . format ( Mixture ( X , Y , Z ) ) + " ▁ litres " ) NEW_LINE DEDENT
Sum of sum of all subsets of a set formed by first N natural numbers | modulo value ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p Update x if it is more than or equal to p ; If y is odd , multiply x with the result ; y must be even now y = y >> 1 y = y / 2 ; function to find ff ( n ) ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver code ; function call
mod = ( int ) ( 1e9 + 7 ) NEW_LINE def power ( x , y , p ) : NEW_LINE INDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT n = n - 1 NEW_LINE ans = n * n NEW_LINE if ( ans >= mod ) : NEW_LINE INDENT ans %= mod NEW_LINE DEDENT ans += n + 2 NEW_LINE if ( ans >= mod ) : NEW_LINE INDENT ans %= mod NEW_LINE DEDENT ans = ( pow ( 2 , n , mod ) % mod * ans % mod ) % mod NEW_LINE ans = ( ans - 1 + mod ) % mod NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( check ( n ) ) NEW_LINE DEDENT
Program to find LCM of 2 numbers without using GCD | Python 3 program to find LCM of 2 numbers without using GCD ; Function to return LCM of two numbers ; Driver Code
import sys NEW_LINE def findLCM ( a , b ) : NEW_LINE INDENT lar = max ( a , b ) NEW_LINE small = min ( a , b ) NEW_LINE i = lar NEW_LINE while ( 1 ) : NEW_LINE INDENT if ( i % small == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT i += lar NEW_LINE DEDENT DEDENT a = 5 NEW_LINE b = 7 NEW_LINE print ( " LCM ▁ of ▁ " , a , " ▁ and ▁ " , b , " ▁ is ▁ " , findLCM ( a , b ) , sep = " " ) NEW_LINE
Smarandache | Python program to print the first ' n ' terms of the Smarandache - Wellin Sequence ; Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Method
from __future__ import print_function NEW_LINE def primes ( n ) : NEW_LINE INDENT i , j = 2 , 0 NEW_LINE result = [ ] NEW_LINE while j < n : NEW_LINE INDENT flag = True NEW_LINE for item in range ( 2 , int ( i ** 0.5 ) + 1 ) : NEW_LINE INDENT if i % item == 0 and i != item : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if flag : NEW_LINE INDENT result . append ( i ) NEW_LINE j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return result NEW_LINE DEDENT def smar_wln ( n ) : NEW_LINE INDENT arr = primes ( n ) NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT for j in range ( 0 , i + 1 ) : NEW_LINE INDENT print ( arr [ j ] , end = ' ' ) NEW_LINE DEDENT print ( end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( ' First { } terms of the Sequence are ' . format ( n ) ) NEW_LINE smar_wln ( n ) NEW_LINE DEDENT
Pentatope number | Function to calculate Pentatope number ; Formula to calculate nth Pentatope number ; Driver Code
def Pentatope_number ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) // 24 ) NEW_LINE DEDENT n = 7 NEW_LINE print ( " % sth ▁ Pentatope ▁ number ▁ : ▁ " % n , Pentatope_number ( n ) ) NEW_LINE n = 12 NEW_LINE print ( " % sth ▁ Pentatope ▁ number ▁ : ▁ " % n , Pentatope_number ( n ) ) NEW_LINE
Program for Centered Icosahedral Number | Function to calculate Centered icosahedral number ; Formula to calculate nth Centered icosahedral number ; Driver Code
def centeredIcosahedralNum ( n ) : NEW_LINE INDENT return ( ( 2 * n + 1 ) * ( 5 * n * n + 5 * n + 3 ) // 3 ) NEW_LINE DEDENT n = 10 NEW_LINE print ( centeredIcosahedralNum ( n ) ) NEW_LINE n = 12 NEW_LINE print ( centeredIcosahedralNum ( n ) ) NEW_LINE
Centered Square Number | Function to calculate Centered square number function ; Formula to calculate nth Centered square number ; Driver Code
def centered_square_num ( n ) : NEW_LINE INDENT return n * n + ( ( n - 1 ) * ( n - 1 ) ) NEW_LINE DEDENT n = 7 NEW_LINE print ( " % sth ▁ Centered ▁ square ▁ number : ▁ " % n , centered_square_num ( n ) ) NEW_LINE
Sum of first n natural numbers | Function to find the sum of series ; Driver code
def seriesSum ( n ) : NEW_LINE INDENT return int ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) NEW_LINE DEDENT n = 4 NEW_LINE print ( seriesSum ( n ) ) NEW_LINE
Dodecagonal number | Function to calculate Dodecagonal number ; Formula to calculate nth Dodecagonal number ; Driver Code
def Dodecagonal_number ( n ) : NEW_LINE INDENT return 5 * n * n - 4 * n NEW_LINE DEDENT n = 7 NEW_LINE print ( Dodecagonal_number ( n ) ) NEW_LINE n = 12 NEW_LINE print ( Dodecagonal_number ( n ) ) NEW_LINE
Finding n | A formula based Python program to find sum of series with cubes of first n natural numbers ; Driver Function
def magicOfSequence ( N ) : NEW_LINE INDENT return ( N * ( N + 1 ) / 2 ) + 2 * N NEW_LINE DEDENT N = 6 NEW_LINE print ( int ( magicOfSequence ( N ) ) ) NEW_LINE
Form a number using corner digits of powers | Storing N raised to power 0 ; Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code
power = [ ] NEW_LINE def nextPower ( N ) : NEW_LINE INDENT global power NEW_LINE carry = 0 NEW_LINE for i in range ( 0 , len ( power ) ) : NEW_LINE INDENT prod = ( power [ i ] * N ) + carry NEW_LINE power [ i ] = prod % 10 NEW_LINE carry = ( int ) ( prod / 10 ) NEW_LINE DEDENT while ( carry ) : NEW_LINE INDENT power . append ( carry % 10 ) NEW_LINE carry = ( int ) ( carry / 10 ) NEW_LINE DEDENT DEDENT def printPowerNumber ( X , N ) : NEW_LINE INDENT global power NEW_LINE power . append ( 1 ) NEW_LINE res = [ ] NEW_LINE for i in range ( 1 , X + 1 ) : NEW_LINE INDENT nextPower ( N ) NEW_LINE res . append ( power [ - 1 ] ) NEW_LINE res . append ( power [ 0 ] ) NEW_LINE DEDENT for i in range ( 0 , len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " " ) NEW_LINE DEDENT DEDENT N = 19 NEW_LINE X = 4 NEW_LINE printPowerNumber ( X , N ) NEW_LINE
First digit in factorial of a number | Python3 program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; Driver Code
import math NEW_LINE def firstDigit ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE while ( fact % 10 == 0 ) : NEW_LINE INDENT fact = int ( fact / 10 ) NEW_LINE DEDENT DEDENT while ( fact >= 10 ) : NEW_LINE INDENT fact = int ( fact / 10 ) NEW_LINE DEDENT return math . floor ( fact ) NEW_LINE DEDENT n = 5 NEW_LINE print ( firstDigit ( n ) ) NEW_LINE
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | Python 3 program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Program
def sumofseries ( n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT res += ( i ) * ( i + 1 ) * ( i + 2 ) NEW_LINE DEDENT return res NEW_LINE DEDENT print ( sumofseries ( 3 ) ) NEW_LINE
Find N Geometric Means between A and B | Python3 program to find n geometric means between A and B ; Prints N geometric means between A and B . ; calculate common ratio ( R ) ; for finding N the Geometric mean between A and B ; Driver Code
import math NEW_LINE def printGMeans ( A , B , N ) : NEW_LINE INDENT R = ( math . pow ( ( B / A ) , 1.0 / ( N + 1 ) ) ) ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( int ( A * math . pow ( R , i ) ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT A = 3 ; NEW_LINE B = 81 ; NEW_LINE N = 2 ; NEW_LINE printGMeans ( A , B , N ) ; NEW_LINE
Numbers having difference with digit sum more than s | function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; driver code
def digitSum ( n ) : NEW_LINE INDENT digSum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digSum += n % 10 NEW_LINE n //= 10 NEW_LINE DEDENT return digSum NEW_LINE DEDENT def countInteger ( n , s ) : NEW_LINE INDENT if ( n < s ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( s , min ( n , s + 163 ) + 1 ) : NEW_LINE INDENT if ( ( i - digitSum ( i ) ) > s ) : NEW_LINE INDENT return ( n - i + 1 ) NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT n = 1000 NEW_LINE s = 100 NEW_LINE print ( countInteger ( n , s ) ) NEW_LINE