Given a positive integer n, return true if n is a Sphenic Number; otherwise, return false. A number is called a Sphenic Number if it is the product of exactly three distinct prime numbers.
Examples:Â
Input: n = 30
Output: true
Explanation: 30 = 2 * 3 * 5 so n is product of 3 distinct prime numbers.Input: n = 60
Output: false
Explanation: 60 = 2 * 2 * 3 * 5 so n is product of 4 prime numbers.
Table of Content
[Naive Approach] Check Every Number as a Prime Factor - O(nân) Time and O(1) Space
The idea is to check every number from 2 to n and identify the prime numbers that divide n. Count the prime factors and make sure each one occurs only once. If exactly three distinct prime factors are found, then n is a Sphenic number.
Working of Approach:
- Traverse all numbers from 2 to n.
- For every divisor, check whether it is prime.
- If it is prime, count it and divide it from n.
- If the same prime divides n again, return false.
- Finally, return true if exactly three distinct prime factors are found.
#include <iostream>
using namespace std;
// Check whether a number is prime.
bool isPrime(int x)
{
if (x < 2)
return false;
// Check all possible divisors.
for (int i = 2; i * i <= x; i++)
{
if (x % i == 0)
return false;
}
return true;
}
bool isSphenicNo(int n)
{
int cnt = 0;
// Check every number as a possible prime factor.
for (int i = 2; i <= n; i++)
{
if (n % i == 0 && isPrime(i))
{
cnt++;
n /= i;
// A repeated prime factor makes it non-sphenic.
if (n % i == 0)
return false;
// More than three distinct prime factors.
if (cnt > 3)
return false;
}
}
// A Sphenic number has exactly three distinct prime factors.
return cnt == 3;
}
int main()
{
int n = 30;
cout << boolalpha << isSphenicNo(n) << endl;
return 0;
}
public class GFG {
// Check whether a number is prime.
public static boolean isPrime(int x)
{
if (x < 2)
return false;
// Check all possible divisors.
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
public static boolean isSphenicNo(int n)
{
int cnt = 0;
// Check every number as a possible prime factor.
for (int i = 2; i <= n; i++) {
if (n % i == 0 && isPrime(i)) {
cnt++;
n /= i;
// A repeated prime factor makes it
// non-sphenic.
if (n % i == 0)
return false;
// More than three distinct prime factors.
if (cnt > 3)
return false;
}
}
// A Sphenic number has exactly three distinct prime
// factors.
return cnt == 3;
}
public static void main(String[] args)
{
int n = 30;
System.out.println(isSphenicNo(n));
}
}
def isPrime(x):
if x < 2:
return False
# Check all possible divisors.
for i in range(2, int(x**0.5) + 1):
if x % i == 0:
return False
return True
def isSphenicNo(n):
cnt = 0
# Check every number as a possible prime factor.
for i in range(2, n + 1):
if n % i == 0 and isPrime(i):
cnt += 1
n //= i
# A repeated prime factor makes it non-sphenic.
if n % i == 0:
return False
# More than three distinct prime factors.
if cnt > 3:
return False
# A Sphenic number has exactly three distinct prime factors.
return cnt == 3
if __name__ == "__main__":
n = 30
print(isSphenicNo(n))
using System;
public class GFG {
// Check whether a number is prime.
public static bool isPrime(int x)
{
if (x < 2)
return false;
// Check all possible divisors.
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
public static bool isSphenicNo(int n)
{
int cnt = 0;
// Check every number as a possible prime factor.
for (int i = 2; i <= n; i++) {
if (n % i == 0 && isPrime(i)) {
cnt++;
n /= i;
// A repeated prime factor makes it
// non-sphenic.
if (n % i == 0)
return false;
// More than three distinct prime factors.
if (cnt > 3)
return false;
}
}
// A Sphenic number has exactly three distinct prime
// factors.
return cnt == 3;
}
public static void Main()
{
int n = 30;
Console.WriteLine(isSphenicNo(n));
}
}
function isPrime(x)
{
if (x < 2)
return false;
// Check all possible divisors.
for (let i = 2; i * i <= x; i++) {
if (x % i === 0)
return false;
}
return true;
}
function isSphenicNo(n)
{
let cnt = 0;
// Check every number as a possible prime factor.
for (let i = 2; i <= n; i++) {
if (n % i === 0 && isPrime(i)) {
cnt++;
n /= i;
// A repeated prime factor makes it non-sphenic.
if (n % i === 0)
return false;
// More than three distinct prime factors.
if (cnt > 3)
return false;
}
}
// A Sphenic number has exactly three distinct prime
// factors.
return cnt === 3;
}
// Driver Code
let n = 30;
console.log(isSphenicNo(n));
Output
true
[Expected Approach] Prime Factorization Using Trial Division - O(ân) Time and O(1) Space
The idea is to factorize n using trial division up to ân, count its distinct prime factors, and immediately return false if any factor occurs more than once. If exactly three distinct prime factors are found, n is a Sphenic number.
Working of Approach:
- Start checking factors from 2 up to ân.
- If i divides n, count it as a distinct prime factor.
- Divide i from n and check whether it divides n again.
- If it occurs again, return false.
- Count any remaining prime factor and check whether the count is exactly 3.
Let us understand with an example:
Input: n = 30
- Initially, cnt = 0 and n = 30.
- i = 2: 30 % 2 == 0, so cnt = 1 and n = 15; 2 does not repeat.
- i = 3: 15 % 3 == 0, so cnt = 2 and n = 5; 3 does not repeat.
- The loop stops because 4 * 4 > 5; since n > 1, count 5 as the third prime factor, so cnt = 3.
- Finally, cnt == 3, so the function returns true.
#include <iostream>
using namespace std;
bool isSphenicNo(int n)
{
int cnt = 0;
// Count distinct prime factors and ensure none repeats.
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
cnt++;
n /= i;
// A repeated prime factor makes the number non-sphenic.
if (n % i == 0)
{
return false;
}
}
}
// Count the remaining prime factor if present.
if (n > 1)
{
cnt++;
}
return cnt == 3;
}
int main()
{
int n = 30;
cout << boolalpha << isSphenicNo(n) << endl;
return 0;
}
import java.util.Scanner;
public class GFG {
public static boolean isSphenicNo(int n)
{
int cnt = 0;
// Count distinct prime factors and ensure none
// repeats.
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
cnt++;
n /= i;
// A repeated prime factor makes the number
// non-sphenic.
if (n % i == 0) {
return false;
}
}
}
// Count the remaining prime factor if present.
if (n > 1) {
cnt++;
}
return cnt == 3;
}
public static void main(String[] args)
{
int n = 30;
System.out.println(isSphenicNo(n));
}
}
def isSphenicNo(n):
cnt = 0
# Count distinct prime factors and ensure none repeats.
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
cnt += 1
n //= i
# A repeated prime factor makes the number non-sphenic.
if n % i == 0:
return False
# Count the remaining prime factor if present.
if n > 1:
cnt += 1
return cnt == 3
if __name__ == "__main__":
n = 30
print(isSphenicNo(n))
using System;
public class GFG {
public static bool isSphenicNo(int n)
{
int cnt = 0;
// Count distinct prime factors and ensure none
// repeats.
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
cnt++;
n /= i;
// A repeated prime factor makes the number
// non-sphenic.
if (n % i == 0) {
return false;
}
}
}
// Count the remaining prime factor if present.
if (n > 1) {
cnt++;
}
return cnt == 3;
}
public static void Main()
{
int n = 30;
Console.WriteLine(isSphenicNo(n));
}
}
function isSphenicNo(n)
{
let cnt = 0;
// Count distinct prime factors and ensure none repeats.
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
cnt++;
n /= i;
// A repeated prime factor makes the number
// non-sphenic.
if (n % i === 0) {
return false;
}
}
}
// Count the remaining prime factor if present.
if (n > 1) {
cnt++;
}
return cnt === 3;
}
// Driver Code
const n = 30;
console.log(isSphenicNo(n));
Output
true