# Count of primes after converting given binary number in base between L to R

Given a binary number **N**, and a range represented by **L**, and **R**, the task is to convert the given binary number in all the base numbers between L and R (L and R inclusive) and count the resulting prime numbers among them.

**Examples:**

Input:N = 111, L = 3, R = 10

Outpu:5

Explanation:

When 111 is interpreted in all the base numbers between 3 and 10 we get the resulting numbers as [21, 13, 12, 11, 10, 7, 7, 7], out of which only 5 numbers are prime. Hence, the output is 5.

Input:N = 11, L = 4, R = 19

Output:16

Explanation:

When 11 is interpreted in all the base numbers between 4 and 19 we get the resulting numbers as 3 which is a prime number. Hence, the output is 16.

**Approch:** Convert the given binary number to each base between **‘L’** and **‘R’** one by one. Thereafter, check if the resulting number is prime or not. If yes, then increment the count.

Below is the implementation of the above approach:

`# Python3 program to count of primes ` `# after converting given binary ` `# number in base between L to R ` ` ` `# Function to interpret the binary number in all ` `# the base numbers between L and R one by one ` `def` `calc(binary, interpret_language): ` ` ` ` ` `# Temporary integer as intermediate ` ` ` `# value in a known language ` ` ` `tmp ` `=` `0` ` ` ` ` `# Represenation of digits in base 2 ` ` ` `base ` `=` `"01"` ` ` ` ` `# For each digit in the 'binary' ` ` ` `# get its index and its value ` ` ` `for` `n, digit ` `in` `enumerate` `(binary[::` `-` `1` `]): ` ` ` `tmp ` `+` `=` `base.index(digit)` `*` `len` `(base)` `*` `*` `n ` ` ` ` ` `# Generate the 'resulting_number' by appending ` ` ` `resulting_number ` `=` `'' ` ` ` ` ` `while` `tmp: ` ` ` `resulting_number ` `+` `=` `interpret_language[tmp ` `%` `len` `(interpret_language)] ` ` ` ` ` `# reduce the value of tmp ` ` ` `tmp ` `/` `/` `=` `len` `(interpret_language) ` ` ` ` ` `return` `resulting_number ` ` ` `# Function to check if the resulting ` `# number is prime or not ` `def` `IS_prime (N): ` ` ` `n ` `=` `int` `(N) ` ` ` `c ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` `c` `+` `=` `1` ` ` ` ` `if` `(c ` `=` `=` `2` `): ` ` ` `return` `1` ` ` ` ` `return` `0` ` ` `# Driver program ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `binary ` `=` `"111"` ` ` `L ` `=` `3` ` ` `R ` `=` `10` ` ` `bases ` `=` `[` `0` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `, ` `8` `, ` `9` `, ` `'A'` `, ` `'B'` `, ` `'D'` `, ` ` ` `'E'` `, ` `'F'` `, ` `'G'` `, ` `'H'` `, ` `'I'` `, ` `'J'` `, ` `'K'` `, ` `'L'` `, ` `'M'` `, ` ` ` `'N'` `, ` `'O'` `, ` `'P'` `, ` `'Q'` `, ` `'R'` `, ` `'S'` `, ` `'T'` `, ` `'U'` `, ` ` ` `'V'` `, ` `'W'` `, ` `'X'` `, ` `'Y'` `, ` `'Z'` `] ` ` ` ` ` `b ` `=` `bases[` `0` `:R] ` ` ` ` ` `# Count all the resulting numbers which are prime ` ` ` `count ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(R` `-` `L ` `+` `1` `): ` ` ` ` ` `# 'list' indicates representation of digits ` ` ` `# in each base number ` ` ` `list` `=` `b[:(L ` `+` `i)] ` ` ` ` ` `# Converting in string ` ` ` `interpret_language ` `=` `''.join(` `map` `(` `str` `, ` `list` `)) ` ` ` ` ` `# Converting the binary number to the respective ` ` ` `# base between L and R ` ` ` `resulting_number ` `=` `calc(binary, interpret_language) ` ` ` `if` `(IS_prime(resulting_number) ` `=` `=` `1` `): ` ` ` `count` `+` `=` `1` ` ` ` ` `print` `(count) ` |

*chevron_right*

*filter_none*

**Output:**

5

## Recommended Posts:

- Converting a Real Number (between 0 and 1) to Binary String
- Given a number N in decimal base, find number of its digits in any base (base b)
- Count number of primes in an array
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Find the Nth digit from right in base B of the given number in Decimal base
- Given a number N in decimal base, find the sum of digits in any base B
- Count the number of primes in the prefix sum array of the given array
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count Primes in Ranges
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Count of interesting primes upto N
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Count numbers which can be represented as sum of same parity primes
- Length of largest sub-array having primes strictly greater than non-primes
- Count of integers up to N which represent a Binary number
- Count number of binary strings without consecutive 1's
- Count number of binary strings of length N having only 0's and 1's

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.