Programming Articles - Page 1485 of 3363

Check if N is Strong Prime in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:21:24

743 Views

Suppose we have a number n. We have to check whether n is a strong prime or not. As we know a number said to be strong prime when it is a prime number that is greater than the average of nearest prime numbers.So, if the input is like num = 37, then the output will be True as nearest prime numbers are 31 and 41, the average is (31+41)/2 = 36. And 37 > 36.To solve this, we will follow these steps −if num is not prime or num is 2, thenreturn Falselast := num - 1, next := ... Read More

Check if n is divisible by power of 2 without using arithmetic operators in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:17:07

466 Views

Suppose we have two numbers x and n. We have to check whether x is divisible by 2^n or not without using arithmetic operators.So, if the input is like x = 32 n = 5, then the output will be True as 32 = 2^5.To solve this, we will follow these steps −if x AND (2^n - 1) is 0, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understanding − Live Demodef solve (x, n):    if (x & ((1

Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:16:26

204 Views

Suppose we have a number n. We have another two numbers a and b. We have to check whether we can generate a number using a and b that divides n.So, if the input is like n = 115, a = 3, b = 2, then the output will be True as 115 is divisible by 23 which is made of 2 and 3.To solve this, we will follow these steps −Define a function util() . This will take temp, a, b, nif temp > n, thenreturn Falseif n is divisible by temp, thenreturn Truereturn true when at least one ... Read More

Check if N is a Factorial Prime in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:15:43

682 Views

Suppose we have a number n, we have to check whether n is a Factorial prime or not. As we know a number is said to be a factorial prime when it is a prime number that is one less than or one more than a factorial of any number.So, if the input is like n = 719, then the output will be True as 719 = 720 - 1 = 6! - 1To solve this, we will follow these steps −if num is not a prime, thenreturn Falsefactorial := 1, i := 1while factorial

Check if N can be represented as sum of integers chosen from set {A, B} in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:09:07

466 Views

Suppose we have a number target. We have another two numbers A and B. We have to check whether we can get target by adding A and B as many times as we want.So, if the input is like Target = 26 A = 5 B = 7, then the output will be True as we can get 26 by adding A and B like (7 + 7 + 7 + 5).To solve this, we will follow these steps −Define a function util() . This will take x, a, b, is_ok, targetif x > target, thenreturnif is_ok[x] is True, thenreturnis_ok[x] ... Read More

Check if moves in a stack or queue are possible or nots in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:08:19

220 Views

Suppose we have one binary list, where 1 denotes push operation and 0 denotes a pop operation on a stack or a queue. We have to check whether the possible set of operations are valid or not.So, if the input is like nums = [1, 0, 1, 1, 0, 1], then the output will be True as the sequence is [Push, Pop, Push, Push, Pop, Push] as we are not popping element from empty list so these operations are valid.To solve this, we will follow these steps −push_count := 0for i in range 0 to size of nums - 1, ... Read More

Check if mirror image of a number is same if displayed in seven segment displays in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:06:56

624 Views

Suppose we have a number n. We have to check whether the mirror image of the number is same as the given number or not when it is displayed on Seven Segment display.So, if the input is like n = 818, then the output will be True.the mirror image is same.To solve this, we will follow these steps −num_str := n as stringfor i in range 0 to size of num_str - 1, doif num_str[i] is not nay of ['0', '1', '8'] then, thenreturn Falseleft := 0right := size of num_str - 1while left < right, doif num_str[left] is not ... Read More

Check if max occurring character of one string appears same no. of times in other in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:01:42

265 Views

Suppose we have two strings s and t, we have to select the most frequent character from s and then we have to check whether that particular character is present in t same number of times or not.So, if the input is like s = "crosssection", t = "securesystem", then the output will be True, as the most frequent character in s is 's'. And there are same number of occurrences of 's' in t.To solve this, we will follow these steps −freq := a map containing all characters of s and their frequenciesmax_freq_char = character in s where frequency ... Read More

Check if Matrix remains unchanged after row reversals in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:01:22

124 Views

Suppose we have a square matrix. We have to check whether the matrix remains same after performing row reversal operations on each row, or not.So, if the input is like686282333then the output will be TrueTo solve this, we will follow these steps −n := row count of matrixfor i in range 0 to n - 1, doleft := 0, right := n - 1while left

Check if matrix can be converted to another matrix by transposing square sub-matrices in Python

Arnab Chakraborty
Updated on 19-Jan-2021 05:01:03

232 Views

Suppose we have two N X M called mat1 and mat2. In an operation, we can transpose any square sub-matrix in mat1. We have to check whether we can get mat2 from mat1 or not by performing given operation.So, if the input is like567123689562173689then the output will be True, because if we get transpose of top right sub-matrix of size 2x2 of mat1, we will get mat2.To solve this, we will follow these steps −row := row count of matricescolumn := column count of matricesfor i in range 0 to row - 1, dotemp1 := a new list, temp2 := ... Read More

Advertisements