- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

The title "Count of primes after converting given binary number in base between L and R" refers to a math problem that involves converting a binary number into a base between L and R and then counting the number of prime numbers that come from the conversion. In math, a prime number is a whole number that is greater than 1 and can only be divided by 1 and itself.

To turn a binary number into a number with a different base, the number has to be written in a different number system. The number system's base is the number of unique digits, and the conversion is done by finding the number's equivalent representation in the new base. Counting prime numbers after they have been converted is a difficult task in number theory that has uses in cryptography, computer science, and other fields. For this problem to be solved, you need to know a lot about number theory, prime numbers, and number systems.

## What is a prime number?

A number is said to be a prime number only when it can be dived by 1 and the number itself. Let's take an example, the number 5 is a prime number because it is divisible only by number1 and 5, while 6 is not a prime number because it is divisble by 2 & 3 as well.

Count of primes is just asking of how many prime numbers are present in a given set of numbers, For example take a set of numbers {1,2,3,4,5,6,7,8,9}, now in this set the number of prime numbers present is 4 which are 2, 3, 5, 7. Also 1 is not a prime number since the only positive divisor for it is the number 1 itself.

### Approaches

There are two main approaches to the count of prime problem which is listed below −

Brute Force Approach

Prime Factorization

## Algorithm

**Step 1** − Take input of the binary number and the range of bases L and R.

**Step 2** − Iterate through each base between L and R (inclusive).

**Step 3** − Convert the binary number into the current base.

**Step 4** − Check if the converted number is a prime number.

**Step 5** − If the converted number is prime, increment the prime count by 1.

**Step 6** − Repeat steps 3-5 for all the bases in the range L to R.

**Step 7** − Return the total count of prime numbers obtained.

Below given is the pseudo code of the algorithm −

input: binary number b, range of bases L and R output: count of prime numbers in the given range Number_of_prime = 0 for base = L to R convert b to base if number_is_prime(converted_number) Number_of_prime ++ return Number_of_prime

number_is_prime() is a method that takes a number as input and returns a Boolean value showing whether the number is prime or not.

## Approach 1: Brute Force Approach

Brute Force Approach involves changing the binary number into each base between L to R and counting the number of prime numbers in each conversion. It needs checking all the possible changes which can be time-consuming for bigger numbers.

The below code includes three functions. The first one is 'isPrime' which returns 1 if the input number is a prime number, else returns 0. The second function 'binaryToDecimal' converts a binary number to decimal. The third function 'countPrimes' counts the number of primes obtained by converting binary numbers between the input range to decimal. Finally, the main function takes input a binary number and a range of numbers, calls the 'countPrimes' function and prints the count of primes.

### Example

This code provides predefined values for the binary number and the range L and R. In this example, I've used the binary number 1010 and the range 5 to 20. You can change these values in the main function as needed.

#include <stdio.h> #include <stdlib.h> #include <math.h> // Function to check if a number is prime or not int isPrime(int n) { int i; for(i = 2; i <= sqrt(n); i++) { if(n%i == 0) { return 0; } } return 1; } // Function to convert binary to decimal int binaryToDecimal(int n) { int decimal = 0, i = 0, remainder; while(n != 0) { remainder = n % 10; n /= 10; decimal += remainder * pow(2, i); ++i; } return decimal; } // Function to count primes in a given range int countPrimes(int L, int R) { int count = 0, i; for(i = L; i <= R; i++) { int decimal = binaryToDecimal(i); if(isPrime(decimal)) { count++; } } return count; } // Main function int main() { int binary = 1010; // Example binary number int L = 5; // Example range lower limit int R = 20; // Example range upper limit // Count primes and print result int count = countPrimes(L, R); printf("Number of primes after converting %d to base between %d and %d is: %d\n", binary, L, R, count); return 0; }

### Output

Number of primes after converting 1010 to base between 5 and 20 is: 7

## Approach 2: Prime Factorization

Prime Factorization includes finding the prime factors of the transformed numbers and checking if they are in the range of prime numbers. It can be an efficient method for smaller numbers but may become computationally expensive for larger numbers.

The below code defines two functions, isPrime() and countPrimes(), which check whether a given number is prime or count the number of primes up to a given number. The main function takes user input for a binary number and base limits, converts the binary number to decimal, and transforms it to different bases within the given limits. For each transformation, the program finds the prime factors and increments a counter if they are within the current base limit. Finally, the program prints the count of primes found. The code imports the standard input/output and boolean libraries.

### Code

#include <stdio.h> #include <stdbool.h> #include <math.h> bool isPrime(int n) { if (n <= 1) { return false; } int i; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } int main() { int binaryNum = 110101; // Predefined binary number input int L = 3; // Predefined lower limit of base int R = 6; // Predefined upper limit of base int decimalNum = 0, base = 1; while (binaryNum > 0) { int digit = binaryNum % 10; decimalNum += digit * base; base *= 2; binaryNum /= 10; } int transformedNum, factor; int primeCount = 0; for (int baseNum = L; baseNum <= R; baseNum++) { transformedNum = decimalNum; while (transformedNum > 1) { for (int i = 2; i <= transformedNum; i++) { if (transformedNum % i == 0) { factor = i; break; } } transformedNum /= factor; if (isPrime(factor) && factor >= baseNum) { primeCount++; } } } printf("Count of primes after converting the given binary number in base between L to R is: %d", primeCount); return 0; }

### Output

Count of primes after converting the given binary number in base between L to R is: 4

## Conclusion

In conclusion, we can determine the count of primes after converting a given binary number into a base between L to R by first turning the binary number into the chosen base and then counting the number of prime numbers within that range.