Found 10476 Articles for Python

Program to check n can be shown as sum of k or not in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:25:30

114 Views

Suppose we have a number n, and another number k. We have to check whether n can be represented as a sum of k prime numbers or not.So, if the input is like n = 30 k = 3, then the output will be True because 30 can be represented like 2 + 11 + 17.To solve this, we will follow these steps −if n < k*2, thenreturn Falseif k > 2, thenreturn Trueif k is same as 2, thenif n is even, thenreturn Trueif (n-2) is prime, thenreturn Truereturn Falseif n is prime, thenreturn Truereturn FalseExampleLet us see the ... Read More

Program to find number of pairs between x, whose multiplication is x and they are coprime in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:22:19

197 Views

Suppose there is a function f(x), that counts number of (p, q) pairs, such that1 < p

Program to find number of possible BSTs can be generated using n distinct nodes in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:17:54

93 Views

Suppose we have a number n. If we have numbers like [1, 2, ..., n] we have to count number ofpossible BSTs can be formed using these n values. If the answer is too large, then mod the result by 10^9+7.So, if the input is like n = 3, then the output will be 14, To solve this, we will follow these stepsa := a list with values [0, 1]m := 10^9+7max_n := 1000for k in range 2 to max_n + 1, doinsert (1 + sum of all elements of the list (a[i] * a[k - i] for all i ... Read More

Program to find out the position of a ball after n reversals in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:12:19

260 Views

Suppose, there are n number of balls. The balls are ordered in a fashion 1, 2, 3, 4, ..., n. Now the balls are reversed in order, or ordered in a way n, n-1, n-2, ......, 2, 1. The balls are again reversed in order, this time they are reversed from position 1 to n, or now the order becomes n, 1, 2, ....., n-1. This reversing process is repeated n times and each time the starting position is moved 1 place to the right. We now have to find out the position of a ball initially in position 'index' ... Read More

Program to reset a polygon to its initial state in Python

Arnab Chakraborty
Updated on 23-Oct-2021 08:05:20

206 Views

Suppose, there is a polygon with n vertices, n flipping axis, and n rotation points. The following are true for the flipping axes and rotation pointsIf n is odd, each flipping axis passes through only one vertex and the middle of the opposite side.If n is even, half of the axes pass through a pair of opposite vertex and the other half passes through a pair of opposite sides.Two following axes have an angle of 360/2n.Now, we rotate the polygon provided. We have n different types of rotators, a k-rotator rotates the polygon at axis k clockwise by (360 x ... Read More

Program to find out the cells containing maximum value in a matrix in Python

Arnab Chakraborty
Updated on 23-Oct-2021 07:58:07

281 Views

Suppose, there is a n x n matrix initialized with 0s. Now, a list is given and it contains some pairs that contain a particular row and a column position. For each item i in the list, the contents of the cells increase by 1 where the row number and the column number are less than the row value and column value of item i in the list. After all the list elements have been traversed, we have to find out the number of cells in the matrix that contains the maximum value. (row and column index start at 0)So, ... Read More

Program to find out number of blocks that can be covered in Python

Arnab Chakraborty
Updated on 23-Oct-2021 07:55:05

316 Views

Suppose there are n blocks in a path, and a worker is putting colored tiles on the blocks. The worker is putting blocks in a way, such that if a block number in the path is divisible by 4 or/and 2 but not 42, he puts a colored tile there. We have to find out the number of blocks he can cover if he has started with k number of colored tiles.So, if the input is like k = 16, then the output will be 32.To solve this, we will follow these steps −MOD = 10^9 + 7quotient := floor ... Read More

Program to apply Russian Peasant Multiplication in Python

Arnab Chakraborty
Updated on 23-Oct-2021 07:52:50

513 Views

Suppose we are given four integer numbers p, q, r, and k. We will use a method called the Russian Peasant Multiplication method and determine the value of (p + q.i)^r = r + s.i. We have to return the value of r mod k and s mod k.So, if the input is like p = 3, q = 0, r = 8, k = 10000, then the output will be (6561, 0) 3^8 = 6561, as q = 0 value of r mod k = 6561.To solve this, we will follow these steps −if r is same as 0, ... Read More

Program to find out the number of integral coordinates on a straight line between two points in Python

Arnab Chakraborty
Updated on 23-Oct-2021 07:50:14

300 Views

Suppose, we have been provided with two points (p1, q1) and (p2, q2). We have to find out the number of integral coordinates (both the x and y values are integers) if a straight line is drawn between the two given points. The number of points is returned.So, if the input is like p1 = 3, q1 = 3, p2 = 6, q2 = 6, then the output will be 2 If we draw the straight line, we will see that the points (5, 5) and (6, 6) are situated on the straight line.To solve this, we will follow these ... Read More

Program to find number of possible moves to start the game to win by the starter in Python

Arnab Chakraborty
Updated on 23-Oct-2021 07:47:51

300 Views

Suppose Amal and Bimal are playing a game. They have n containers with one or more chocolates inside it. These containers are numbered from 1 to N, where ith container has count[i] number of chocolates. Now the game is like. First player will select a container and take one or more chocolates from it. Then the second player will select a non-empty container and take one or more chocolates from it, like this they play alternatively. When one of the players has no way to take any chocolates, then he/she loses the game. If Amal's turn is first we have ... Read More

Advertisements