Related Articles
Count of primes after converting given binary number in base between L to R
• Last Updated : 30 Jul, 2020

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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: 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)`
Output:
```5
``` My Personal Notes arrow_drop_up