Python Articles

Page 97 of 855

Check whether the frequencies of all the characters in a string are prime or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 267 Views

Suppose we have a string s. We have to check whether the occurrences of each character in s is prime or notSo, if the input is like s = "apuuppa", then the output will be True as there are two 'a's, three 'p's and two 'u's.To solve this, we will follow these steps −freq := a map containing all characters and their frequenciesfor each char in freq, doif freq[char] > 0 and freq[char] is not prime, thenreturn Falsereturn TrueLet us see the following implementation to get better understanding −Example Codefrom collections import defaultdict def isPrime(num):    if num > ...

Read More

Check whether the given number is Euclid Number or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 313 Views

Suppose we have a number n. We have to check whether n is Euclid number or not. As we know Euclid numbers are integer which can be represented as n= Pn+1where  is product of first n prime numbers.So, if the input is like n = 211, then the output will be True n can be represented as 211=(2×3×5×7)+1To solve this, we will follow these steps −MAX := 10000primes := a new listDefine a function generate_all_primes() . This will takeprime := a list of size MAX and fill with Truex := 2while x * x < MAX, doif prime[x] is True, thenfor i ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 192 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 Codedef isPrime(num):   ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 386 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 Codedef isPrime(num): ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 258 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 Codeclass ListNode:    def __init__(self, data, next = None):       self.val = data     ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 611 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 Codefrom math import sqrt def solve(n):    res_num ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 258 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 point (x, y) lies on a given line in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ 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 sum of absolute difference of adjacent digits is Prime or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 262 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 sum of prime elements of the array is prime or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 227 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
Showing 961–970 of 8,547 articles
« Prev 1 95 96 97 98 99 855 Next »
Advertisements