Blum Integer is a semi-prime number, suppose p and q are the two factors (i.e. n = p * q), they(p and q) are of the form 4t + 3, where t is some integer.
First few Blum Integers are 21, 33, 57, 69, 77, 93, 129, 133, 141, 161, 177, ...
Note: Because of the condition that both the factors should be semi-primes, even numbers can not be Blum integers neither can be the numbers below 20,
So we have to check only for an odd integer greater than 20 if it is a Blum Integer or not.
Examples :
Input: 33
Output: Yes
Explanation: 33 = 3 * 11, 3 and 11 are both
semi-primes as well as of the form 4t + 3
for t = 0, 2
Input: 77
Output: Yes
Explanation: 77 = 7 * 11, 7 and 11 both are
semi-prime as well as of the form 4t + 3
for t = 1, 2
Input: 25
Output: No
Explanation: 25 = 5*5, 5 and 5 are both
semi-prime but are not of the form 4t
+ 3, Hence 25 is not a Blum integer.
Approach: For a given odd integer greater than 20, we calculate the prime numbers from 1 to that odd integer. If we find any prime number that divides that odd integer and its quotient both are prime and follow the form 4t + 3 for some integer, then the given odd integer is Blum Integer.
Below is the implementation of above approach :
C++
// CPP program to check if a number is a Blum
// integer
#include <bits/stdc++.h>
using namespace std;
// Function to cheek if number is Blum Integer
bool isBlumInteger(int n)
{
bool prime[n + 1];
memset(prime, true, sizeof(prime));
// to store prime numbers from 2 to n
for (int i = 2; i * i <= n; i++) {
// If prime[i] is not
// changed, then it is a prime
if (prime[i] == true) {
// Update all multiples of p
for (int j = i * 2; j <= n; j += i)
prime[j] = false;
}
}
// to check if the given odd integer
// is Blum Integer or not
for (int i = 2; i <= n; i++) {
if (prime[i]) {
// checking the factors
// are of 4t+3 form or not
if ((n % i == 0) && ((i - 3) % 4) == 0) {
int q = n / i;
return (q != i && prime[q] &&
(q - 3) % 4 == 0);
}
}
}
return false;
}
// driver code
int main()
{
// give odd integer greater than 20
int n = 249;
if (isBlumInteger(n))
cout << "Yes";
else
cout << "No";
}
Java
// Java implementation to check If
// a number is a Blum integer
import java.util.*;
class GFG {
public static boolean isBlumInteger(int n)
{
boolean prime[] = new boolean[n + 1];
for (int i = 0; i < n; i++)
prime[i] = true;
// to store prime numbers from 2 to n
for (int i = 2; i * i <= n; i++) {
// If prime[i] is not changed,
// then it is a prime
if (prime[i] == true) {
// Update all multiples of p
for (int j = i * 2; j <= n; j += i)
prime[j] = false;
}
}
// to check if the given odd integer
// is Blum Integer or not
for (int i = 2; i <= n; i++) {
if (prime[i]) {
// checking the factors are
// of 4t + 3 form or not
if ((n % i == 0) && ((i - 3) % 4) == 0) {
int q = n / i;
return (q != i && prime[q] &&
(q - 3) % 4 == 0);
}
}
}
return false;
}
// driver code
public static void main(String[] args)
{
// give odd integer greater than 20
int n = 249;
if (isBlumInteger(n) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
Python3
# python 3 program to check if a
# number is a Blum integer
# Function to cheek if number
# is Blum Integer
def isBlumInteger(n):
prime = [True]*(n + 1)
# to store prime numbers from 2 to n
i = 2
while (i * i <= n):
# If prime[i] is not
# changed, then it is a prime
if (prime[i] == True) :
# Update all multiples of p
for j in range(i * 2, n + 1, i):
prime[j] = False
i = i + 1
# to check if the given odd integer
# is Blum Integer or not
for i in range(2, n + 1) :
if (prime[i]) :
# checking the factors
# are of 4t+3 form or not
if ((n % i == 0) and
((i - 3) % 4) == 0):
q = int(n / i)
return (q != i and prime[q]
and (q - 3) % 4 == 0)
return False
# driver code
# give odd integer greater than 20
n = 249
if (isBlumInteger(n)):
print("Yes")
else:
print("No")
# This code is contributed by Smitha.
C#
// C# implementation to check If
// a number is a Blum integer
using System;
class GFG {
public static bool isBlumInteger(int n)
{
bool[] prime = new bool[n + 1];
for (int i = 0; i < n; i++)
prime[i] = true;
// to store prime numbers from 2 to n
for (int i = 2; i * i <= n; i++) {
// If prime[i] is not changed,
// then it is a prime
if (prime[i] == true) {
// Update all multiples of p
for (int j = i * 2; j <= n; j += i)
prime[j] = false;
}
}
// to check if the given odd integer
// is Blum Integer or not
for (int i = 2; i <= n; i++) {
if (prime[i]) {
// checking the factors are
// of 4t + 3 form or not
if ((n % i == 0) && ((i - 3) % 4) == 0)
{
int q = n / i;
return (q != i && prime[q] &&
(q - 3) % 4 == 0);
}
}
}
return false;
}
// Driver code
static public void Main ()
{
// give odd integer greater than 20
int n = 249;
if (isBlumInteger(n) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Ajit.
PHP
<?php
// PHP program to check if a
// number is a Blum integer
// Function to cheek if
// number is Blum Integer
function isBlumInteger($n)
{
$prime = array_fill(0, $n + 1, true);
// to store prime
// numbers from 2 to n
for ($i = 2; $i * $i <= $n; $i++)
{
// If prime[i] is not
// changed, then it is a prime
if ($prime[$i] == true)
{
// Update all multiples of p
for ($j = $i * 2; $j <= $n; $j += $i)
$prime[$j] = false;
}
}
// to check if the given
// odd integer is Blum
// Integer or not
for ($i = 2; $i <= $n; $i++)
{
if ($prime[$i])
{
// checking the factors
// are of 4t+3 form or not
if (($n % $i == 0) &&
(($i - 3) % 4) == 0)
{
$q = (int)$n / $i;
return ($q != $i && $prime[$q] &&
($q - 3) % 4 == 0);
}
}
}
return false;
}
// Driver code
// give odd integer
// greater than 20
$n = 249;
if (isBlumInteger($n))
echo "Yes";
else
echo "No";
// This code is contributed by mits.
?>
JavaScript
<script>
// Javascript implementation to check If
// a number is a Blum integer
function isBlumInteger(n)
{
let prime = new Array(n + 1);
for(let i = 0; i < n; i++)
prime[i] = true;
// To store prime numbers from 2 to n
for(let i = 2; i * i <= n; i++)
{
// If prime[i] is not changed,
// then it is a prime
if (prime[i] == true)
{
// Update all multiples of p
for(let j = i * 2; j <= n; j += i)
prime[j] = false;
}
}
// To check if the given odd integer
// is Blum Integer or not
for(let i = 2; i <= n; i++)
{
if (prime[i])
{
// Checking the factors are
// of 4t + 3 form or not
if ((n % i == 0) && ((i - 3) % 4) == 0)
{
let q = parseInt(n / i, 10);
return (q != i && prime[q] &&
(q - 3) % 4 == 0);
}
}
}
return false;
}
// Driver code
// Give odd integer greater than 20
let n = 249;
if (isBlumInteger(n) == true)
document.write("Yes");
else
document.write("No");
// This code is contributed by decode2207
</script>
Time Complexity: O(nsqrtn)
Auxiliary Space: O(n)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Similar Reads
Complement of Base 10 Integer
Given a base 10 integer N, the task is to find the 1's complement of this Base 10 Integer. Examples: Input: N = 5Output: 2Explanation: Binary representation of 5 is "101". Its one's complement is "010" = 2. Input: N = 255Output: 0 Approach: Here the number is converted by flipping bits and adding th
3 min read
Count set bits in an integer
Write an efficient program to count the number of 1s in the binary representation of an integer.Examples : Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 13 is 1101 and has 3 set bits[Naive Approach] - One by One CountingTh
15+ min read
Baum Sweet Sequence
Baum Sweet Sequence is an infinite binary sequence of 0s and 1s. The nth term of the sequence is 1 if the number n has no odd number of contiguous zeroes in its binary representation, else the nth term is 0. The first few terms of the sequence are: b1 = 1 (binary of 1 is 1) b2 = 0 (binary of 2 is 10
5 min read
Evil Number
An evil number is a non-negative number that has an even number of 1s in its binary expansion. (Binary Expansion â is representation of a number in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one)). Odious
9 min read
K'th Boom Number
Boom numbers are numbers consisting only of digits 2 and 3. Given an integer k (0<k<=10^7) , display the k-th Boom number. Examples: Input : k = 2 Output: 3 Input : k = 3 Output: 22 Input : k = 100 Output: 322323 Input: k = 1000000 Output: 3332322223223222223Recommended PracticeKth boom number
7 min read
Unset the last m bits
Given a non-negative number n. The problem is to unset the last m bits in the binary representation of n.Constraint: 1 <= m <= num, where num is the number of bits in the binary representation of n. Examples: Input : n = 10, m = 2 Output : 8 (10)10 = (1010)2 (8)10 = (1000)2 The last two bits i
6 min read
Carol Number
A Carol number is an integer of the form 4n - 2(n+1) - 1. An equivalent formula is (2n-1)2 - 2.An Interesting Property : For n > 2, the binary representation of the n-th Carol number is n-2 consecutive one's, a single zero in the middle, and n + 1 more consecutive one's. Example, n = 4 carol numb
3 min read
Lynch-Bell Numbers
Lynch-Bell Number is a number N if its digits are all distinct and N is divisible by each of it's digit. 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24... Given a number N, the task is to check if N is an Lynch-Bell Number or not. If N is an Lynch-Bell Number then print "Yes" else print "No".Examples: Input:
10 min read
Check if a number is Bleak
A number 'n' is called Bleak if it cannot be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x.Examples : Input : n = 3 Output : false 3 is not Bleak as it can be represented as 2 + countSetBits(2). Input : n =
12 min read
Dudeney Numbers
Given an integer n, the task is to check if n is a Dudeney number or not. A Dudeney number is a positive integer that is a perfect cube such that the sum of its decimal digits is equal to the cube root of the number. Examples: Input: N = 19683 Output: Yes 19683 = 273 and 1 + 9 + 6 + 8 + 3 = 27 Input
5 min read