Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 
Output:
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. 
 

 

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:
 

C++




// C++ program for the above approach
 
#include<bits/stdc++.h>
using namespace std;
 
// Function to interpret the binary number in all
// the base numbers between L and R one by one
string calc(string binary, string interpret_language)
{
    // Temporary integer as intermediate
    // value in a known language
    int tmp = 0;
 
    // Representation of digits in base 2
    string base = "01";
 
    // For each digit in the 'binary'
    // get its index and its value
    for (int n = 0; n < binary.size(); n++)
    {
        char digit = binary[binary.size() - 1 - n];
        tmp += base.find(digit) * pow(base.size(), n);
    }
 
    // Generate the 'resulting_number' by appending
    string resulting_number = "";
    while (tmp)
    {
        resulting_number += interpret_language[tmp % interpret_language.size()];
        // reduce the value of tmp
        tmp /= interpret_language.size();
    }
 
    // Reverse the resulting string
    reverse(resulting_number.begin(), resulting_number.end());
 
    return resulting_number;
}
 
// Function to check if the resulting
// number is prime or not
bool is_prime(string N)
{
    int n = stoi(N);
    int c = 1;
 
    for (int i = 2; i <= n; i++)
    {
        if (n % i == 0)
        {
            c++;
        }
    }
 
    if (c == 2)
    {
        return true;
    }
 
    return false;
}
 
// Driver program
int main()
{
    string binary = "111";
    int L = 3;
    int R = 10;
    vector<char> 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'};
 
    vector<char> b(bases.begin(), bases.begin() + R);
 
    // Count all the resulting numbers which are prime
    int count = 0;
 
    for (int i = 0; i <= R - L; i++)
    {
        // 'list' indicates representation of digits
        // in each base number
        vector<char> list(b.begin(), b.begin() + (L + i));
 
        // Converting in string
        string interpret_language(list.begin(), list.end());
 
        // Converting the binary number to the respective
        // base between L and R
        string resulting_number = calc(binary, interpret_language);
        if (is_prime(resulting_number))
        {
            count++;
        }
    }
 
    cout << count << endl;
 
    return 0;
}
 
 
// This code is contributed by adityashatmfh


Java




// Java program for the above approach
 
import java.util.*;
 
class Main {
     
    // Function to interpret the binary number in all
    // the base numbers between L and R one by one
    static String calc(String binary, String interpret_language) {
        // Temporary integer as intermediate
        // value in a known language
        int tmp = 0;
         
        // Representation of digits in base 2
        String base = "01";
         
        // For each digit in the 'binary'
        // get its index and its value
        for (int n = 0; n < binary.length(); n++) {
            char digit = binary.charAt(binary.length() - 1 - n);
            tmp += base.indexOf(digit) * Math.pow(base.length(), n);
        }
         
        // Generate the 'resulting_number' by appending
        String resulting_number = "";
        while (tmp != 0) {
            resulting_number += interpret_language.charAt(tmp % interpret_language.length());
            // reduce the value of tmp
            tmp /= interpret_language.length();
        }
         
        // Reverse the resulting string
        resulting_number = new StringBuilder(resulting_number).reverse().toString();
         
        return resulting_number;
    }
     
    // Function to check if the resulting
    // number is prime or not
    static boolean is_prime(String N) {
        int n = Integer.parseInt(N);
        int c = 1;
         
        for (int i = 2; i <= n; i++) {
            if (n % i == 0) {
                c++;
            }
        }
         
        if (c == 2) {
            return true;
        }
         
        return false;
    }
     
    // Driver program
    public static void main(String[] args) {
        String binary = "111";
        int L = 3;
        int R = 10;
        List<Character> bases = Arrays.asList('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');
         
        List<Character> b = bases.subList(0, R);
         
        // Count all the resulting numbers which are prime
        int count = 0;
         
        for (int i = 0; i <= R - L; i++) {
            // 'list' indicates representation of digits
            // in each base number
            List<Character> list = b.subList(0, L + i);
             
            // Converting in string
            String interpret_language = "";
            for (char c : list) {
                interpret_language += c;
            }
             
            // Converting the binary number to the respective
            // base between L and R
            String resulting_number = calc(binary, interpret_language);
            if (is_prime(resulting_number)) {
                count++;
            }
        }
         
        System.out.println(count);
    }
}
 
// This code is contributed by codebraxnzt


Python3




# 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
     
    # Representation 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)


Javascript




// javascript code for the above approach
 
// Function to interpret the binary number in all
// the base numbers between L and R one by one
function calc(binary, interpret_language) {
 
  // Temporary integer as intermediate
  // value in a known language
  let tmp = 0;
   
  // Representation of digits in base 2
  const base = "01";
   
  // For each digit in the 'binary'
  // get its index and its value
  for (let n = 0; n < binary.length; n++) {
    const digit = binary[binary.length - 1 - n];
    tmp += base.indexOf(digit) * Math.pow(base.length, n);
  }
   
  // Generate the 'resulting_number' by appending
  let resulting_number = "";
  while (tmp) {
   
    resulting_number += interpret_language[tmp % interpret_language.length];
     
    // reduce the value of tmp
    tmp = Math.floor(tmp / interpret_language.length);
  }
  return resulting_number;
}
 
// Function to check if the resulting
// number is prime or not
function isPrime(N) {
  const n = parseInt(N);
  let c = 1;
  for (let i = 2; i <= n; i++) {
    if (n % i == 0) {
      c += 1;
    }
  }
  if (c == 2) {
    return 1;
  }
  return 0;
}
 
// Driver program
const binary = "111";
const L = 3;
const R = 10;
const 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"];
const b = bases.slice(0, R);
 
// Count all the resulting numbers which are prime
let count = 0;
for (let i = 0; i <= R - L; i++) {
 
  // 'list' indicates representation of digits
  // in each base number
  const list = b.slice(0, L + i);
   
  // Converting in string
  const interpret_language = list.join("");
   
  // Converting the binary number to the respective
  // base between L and R
  const resulting_number = calc(binary, interpret_language);
  if (isPrime(resulting_number) == 1) {
    count += 1;
  }
}
console.log(count);
 
// This code is contributed by Prince Kumar


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG { // Function to interpret the binary number in
            // all
    // the base numbers between L and R one by one
    static string Calc(string binary,
                       string interpret_language)
    {
        // Temporary integer as intermediate
        // value in a known language
        int tmp = 0;
 
        // Representation of digits in base 2
        string baseString = "01";
 
        // For each digit in the 'binary'
        // get its index and its value
        for (int n = 0; n < binary.Length; n++) {
            char digit = binary[binary.Length - 1 - n];
            tmp += baseString.IndexOf(digit)
                   * (int)Math.Pow(baseString.Length, n);
        }
 
        // Generate the 'resulting_number' by appending
        string resulting_number = "";
        while (tmp > 0) {
            resulting_number += interpret_language
                [tmp % interpret_language.Length];
            // reduce the value of tmp
            tmp /= interpret_language.Length;
        }
 
        // Reverse the resulting string
        char[] resultChars = resulting_number.ToCharArray();
        Array.Reverse(resultChars);
        resulting_number = new string(resultChars);
 
        return resulting_number;
    }
 
    // Function to check if the resulting
    // number is prime or not
    static bool IsPrime(string N)
    {
        int n = Int32.Parse(N);
        int c = 1;
 
        for (int i = 2; i <= n; i++) {
            if (n % i == 0) {
                c++;
            }
        }
 
        if (c == 2) {
            return true;
        }
 
        return false;
    }
 
    // Driver program
    static void Main(string[] args)
    {
        string binary = "111";
        int L = 3;
        int R = 10;
        List<char> bases = new List<char>{
            '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'
        };
 
        List<char> b = bases.GetRange(0, R);
 
        // Count all the resulting numbers which are prime
        int count = 0;
 
        for (int i = 0; i <= R - L; i++) {
            // 'list' indicates representation of digits
            // in each base number
            List<char> list = b.GetRange(0, L + i);
 
            // Converting in string
            string interpret_language
                = new string(list.ToArray());
 
            // Converting the binary number to the
            // respective base between L and R
            string resulting_number
                = Calc(binary, interpret_language);
            if (IsPrime(resulting_number)) {
                count++;
            }
        }
 
        Console.WriteLine(count);
    }
}


Output: 

5

 



Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads