Server Side Programming Articles - Page 1360 of 2650

Check whether the sum of prime elements of the array is prime or not in Python

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

181 Views

Suppose we have an array nums. We have to check whether the sum of all prime elements in the given array is also prime or notSo, if the input is like nums = [1, 2, 4, 5, 3, 3], then the output will be True as sum of all primes are (2+5+3+3) = 13 and 13 is also prime.To solve this, we will follow these steps −MAX := 10000sieve := a list of size MAX and fill with trueDefine a function generate_list_of_primes()sieve[0] := False, sieve[1] := Falsefor i in range 2 to MAX - 1, doif sieve[i] is true, thenfor ... Read More

Check whether the sum of absolute difference of adjacent digits is Prime or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:01:44

222 Views

Suppose we have a number n. We have to check whether the sum of the absolute difference of adjacent digit pairs is prime or not.So, if the input is like n = 574, then the output will be True as |5-7| + |7-4| = 5, this is prime.To solve this, we will follow these steps −num_str := n as stringtotal := 0for i in range 1 to size of num_str - 1, dototal := total + |digit at place num_str[i - 1] - digit at place num_str[i]|if total is prime, thenreturn Truereturn FalseLet us see the following implementation to get ... Read More

Check whether the point (x, y) lies on a given line in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:00:59

2K+ Views

Suppose we have a straight line in the form y = mx + b, where m is slope and b is y-intercept. And have another coordinate point (x, y). We have to check whether this coordinate point lies on that straight line or not.So, if the input is like m = 3 b = 5 point = (6, 23), then the output will be True as if we put the given x and y coordinate values on the straight line equation then it will satisfy.To solve this, we will follow these steps −if y of point is same as (m ... Read More

Check whether the number has only first and last bits set in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:59:42

187 Views

Suppose we have a number n. We have to check whether the number has only two set bits at first and last position or not.So, if the input is like n = 17, then the output will be True as the binary representation of n is 10001, there is only two 1's at first and last position.To solve this, we will follow these steps −if n is same as 1, thenreturn Truereturn true if n - 1 is power of 2, otherwise falseLet us see the following implementation to get better understanding −Example CodeLive Demodef is_pow_of_two(n):    return (n & ... Read More

Check whether the number formed by concatenating two numbers is a perfect square or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:59:00

194 Views

Suppose we have two numbers x and y. We have to concatenate them and check whether the resultant number is perfect square or not.So, if the input is like x = 2 y = 89, then the output will be True as after concatenating the number will be 289 which is 17^2.To solve this, we will follow these steps −first_num := x as stringsecond_num := y as stringres_num := concatenate first_num and second_num then convert to integersqrt_val := integer part of square root of(res_num)if sqrt_val * sqrt_val is same as res_num, thenreturn Truereturn FalseLet us see the following implementation to ... Read More

Check whether the number can be made perfect square after adding 1 in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:58:32

568 Views

Suppose we have a number n. We have to check whether the number can be a perfect square number by adding 1 with it or not.So, if the input is like n = 288, then the output will be True as after adding 1, it becomes 289 which is same as 17^2.To solve this, we will follow these steps −res_num := n + 1sqrt_val := integer part of square root of(res_num)if sqrt_val * sqrt_val is same as res_num, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demofrom math import sqrt def solve(n):   ... Read More

Check whether the length of given linked list is Even or Odd in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:57:08

217 Views

Suppose we have a linked list, we have to check whether the length of it is odd or even.So, if the input is like head = [5, 8, 7, 4, 3, 6, 4, 5, 8], then the output will be Odd.To solve this, we will follow these steps −while head is not null and next of head is not null, dohead := next of next of headif head is null, thenreturn "Even"return "Odd"Let us see the following implementation to get better understanding −Example CodeLive Democlass ListNode:    def __init__(self, data, next = None):       self.val = data   ... Read More

Check whether the given string is a valid identifier in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:56:34

868 Views

Suppose we have a string representing an identifier. We have to check whether it is valid or not. There are few criteria based on which we can determine whether it is valid or not.It must start with underscore '_' or any uppercase or lowercase lettersIt does not contain any whitespaceAll the subsequent characters after the first one must not consist of any special characters like $, #, % etc.If all of these three are valid then only the string is valid identifier.So, if the input is like id = "_hello_56", then the output will be True.To solve this, we will ... Read More

Check whether the given numbers are Cousin prime or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:55:30

342 Views

Suppose we have a pair of integers. We have to check whether they are cousin primes or not. Two numbers are said to be cousin primes when both are primes and differ by 4.So, if the input is like pair = (19, 23), then the output will be True as these are two primes and their difference is 4 so they are cousin primes.To solve this, we will follow these steps −if difference between two elements is not 4, thenreturn Falsereturn true when both are prime, otherwise falseLet us see the following implementation to get better understanding −Example CodeLive Demodef ... Read More

Check whether the given number is Wagstaff prime or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:53:55

149 Views

Suppose we have a number n. We have to check whether n is Wagstaff prime or not. As we know Wagstaff prime is a prime number which is in the following form.where q is an odd prime number.So, if the input is like n = 683, then the output will be True n can be represented asSo here q = 11. And q is odd prime.To solve this, we will follow these steps −if num is prime and (num*3 - 1) is also prime, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef isPrime(num): ... Read More

Advertisements