Given a number 'n', check whether it is a hoax number or not.
A Hoax Number is defined as a composite number, whose sum of digits is equal to the sum of digits of its distinct prime factors. It may be noted here that, 1 is not considered a prime number, hence it is not included in the sum of digits of distinct prime factors.
Examples :
Input : 22
Output : A Hoax Number
Explanation : The distinct prime factors of 22
are 2 and 11. The sum of their digits are 4,
i.e 2 + 1 + 1 and sum of digits of 22 is also 4.
Input : 84
Output : A Hoax Number
Explanation : The distinct prime factors of 84
are 2, 3 and 7. The sum of their digits are 12,
i.e 2 + 3 + 4 and sum of digits of 84 is also 12.
Input : 19
Output : Not a Hoax Number
Explanation : By definition, a hoax number is
a composite number.
The definition of Hoax numbers bears close resemblance with the definition of Smith Numbers. In fact, some of the Hoax numbers are also Smith numbers. It is apparent that those hoax numbers that do not have repeated factors in their prime decomposition, i.e square free number are also eligible Smith numbers.
Implementation
1) First generate all distinct prime factors of the number 'n'.
2) If the 'n' is not a prime number, find the sum of digits of the factors obtained in step 1.
3) Find the sum of digits of 'n'.
4) Check if the sums obtained in steps 2 and 3 are equal or not.
5) If the sums are equal, 'n' is a hoax number.
C++
// CPP code to check if a number is a hoax
// number or not.
#include <bits/stdc++.h>
using namespace std;
// Function to find distinct prime factors
// of given number n
vector<int> primeFactors(int n)
{
vector<int> res;
if (n % 2 == 0) {
while (n % 2 == 0)
n = n / 2;
res.push_back(2);
}
// n is odd at this point, since it is no
// longer divisible by 2. So we can test
// only for the odd numbers, whether they
// are factors of n
for (int i = 3; i <= sqrt(n); i = i + 2) {
// Check if i is prime factor
if (n % i == 0) {
while (n % i == 0)
n = n / i;
res.push_back(i);
}
}
// This condition is to handle the case
// when n is a prime number greater than 2
if (n > 2)
res.push_back(n);
return res;
}
// Function to calculate sum of digits of
// distinct prime factors of given number n
// and sum of digits of number n and compare
// the sums obtained
bool isHoax(int n)
{
// Distinct prime factors of n are being
// stored in vector pf
vector<int> pf = primeFactors(n);
// If n is a prime number, it cannot be a
// hoax number
if (pf[0] == n)
return false;
// Finding sum of digits of distinct prime
// factors of the number n
int all_pf_sum = 0;
for (int i = 0; i < pf.size(); i++) {
// Finding sum of digits in current
// prime factor pf[i].
int pf_sum;
for (pf_sum = 0; pf[i] > 0;
pf_sum += pf[i] % 10, pf[i] /= 10)
;
all_pf_sum += pf_sum;
}
// Finding sum of digits of number n
int sum_n;
for (sum_n = 0; n > 0; sum_n += n % 10,
n /= 10)
;
// Comparing the two calculated sums
return sum_n == all_pf_sum;
}
// Driver Method
int main()
{
int n = 84;
if (isHoax(n))
cout << "A Hoax Number\n";
else
cout << "Not a Hoax Number\n";
return 0;
}
Java
// Java code to check if a number is
// a hoax number or not.
import java.io.*;
import java.util.*;
public class GFG {
// Function to find distinct
// prime factors of given
// number n
static List<Integer> primeFactors(int n)
{
List<Integer> res = new ArrayList<Integer>();
if (n % 2 == 0)
{
while (n % 2 == 0)
n = n / 2;
res.add(2);
}
// n is odd at this point,
// since it is no longer
// divisible by 2. So we
// can test only for the
// odd numbers, whether they
// are factors of n
for (int i = 3; i <= Math.sqrt(n);
i = i + 2)
{
// Check if i is prime factor
if (n % i == 0)
{
while (n % i == 0)
n = n / i;
res.add(i);
}
}
// This condition is to
// handle the case when
// n is a prime number
// greater than 2
if (n > 2)
res.add(n);
return res;
}
// Function to calculate
// sum of digits of distinct
// prime factors of given
// number n and sum of
// digits of number n and
// compare the sums obtained
static boolean isHoax(int n)
{
// Distinct prime factors
// of n are being
// stored in vector pf
List<Integer> pf = primeFactors(n);
// If n is a prime number,
// it cannot be a hoax number
if (pf.get(0) == n)
return false;
// Finding sum of digits of distinct
// prime factors of the number n
int all_pf_sum = 0;
for (int i = 0; i < pf.size(); i++)
{
// Finding sum of digits in current
// prime factor pf[i].
int pf_sum;
for (pf_sum = 0; pf.get(i) > 0;
pf_sum += pf.get(i) % 10,
pf.set(i,pf.get(i) / 10));
all_pf_sum += pf_sum;
}
// Finding sum of digits of number n
int sum_n;
for (sum_n = 0; n > 0; sum_n += n % 10,
n /= 10)
;
// Comparing the two calculated sums
return sum_n == all_pf_sum;
}
// Driver Code
public static void main(String args[])
{
int n = 84;
if (isHoax(n))
System.out.print( "A Hoax Number\n");
else
System.out.print("Not a Hoax Number\n");
}
}
// This code is contributed by
// Manish Shaw (manishshaw1)
Python3
# Python3 code to check if a number is a hoax
# number or not.
import math
# Function to find distinct prime factors
# of given number n
def primeFactors(n) :
res = []
if (n % 2 == 0) :
while (n % 2 == 0) :
n = int(n / 2)
res.append(2)
# n is odd at this point, since it is no
# longer divisible by 2. So we can test
# only for the odd numbers, whether they
# are factors of n
for i in range(3,int(math.sqrt(n)),2):
# Check if i is prime factor
if (n % i == 0) :
while (n % i == 0) :
n = int(n / i)
res.append(i)
# This condition is to handle the case
# when n is a prime number greater than 2
if (n > 2) :
res.append(n)
return res
# Function to calculate sum of digits of
# distinct prime factors of given number n
# and sum of digits of number n and compare
# the sums obtained
def isHoax(n) :
# Distinct prime factors of n are being
# stored in vector pf
pf = primeFactors(n)
# If n is a prime number, it cannot be a
# hoax number
if (pf[0] == n) :
return False
# Finding sum of digits of distinct prime
# factors of the number n
all_pf_sum = 0
for i in range(0,len(pf)):
# Finding sum of digits in current
# prime factor pf[i].
pf_sum = 0
while (pf[i] > 0):
pf_sum += pf[i] % 10
pf[i] = int(pf[i] / 10)
all_pf_sum += pf_sum
# Finding sum of digits of number n
sum_n = 0;
while (n > 0):
sum_n += n % 10
n = int(n / 10)
# Comparing the two calculated sums
return sum_n == all_pf_sum
# Driver Method
n = 84;
if (isHoax(n)):
print ("A Hoax Number\n")
else:
print ("Not a Hoax Number\n")
# This code is contributed by Manish Shaw
# (manishshaw1)
C#
// C# code to check if a number is
// a hoax number or not.
using System;
using System.Collections.Generic;
class GFG {
// Function to find distinct
// prime factors of given
// number n
static List<int> primeFactors(int n)
{
List<int> res = new List<int>();
if (n % 2 == 0)
{
while (n % 2 == 0)
n = n / 2;
res.Add(2);
}
// n is odd at this point,
// since it is no longer
// divisible by 2. So we
// can test only for the
// odd numbers, whether they
// are factors of n
for (int i = 3; i <= Math.Sqrt(n);
i = i + 2)
{
// Check if i is prime factor
if (n % i == 0)
{
while (n % i == 0)
n = n / i;
res.Add(i);
}
}
// This condition is to
// handle the case when
// n is a prime number
// greater than 2
if (n > 2)
res.Add(n);
return res;
}
// Function to calculate
// sum of digits of distinct
// prime factors of given
// number n and sum of
// digits of number n and
// compare the sums obtained
static bool isHoax(int n)
{
// Distinct prime factors
// of n are being
// stored in vector pf
List<int> pf = primeFactors(n);
// If n is a prime number,
// it cannot be a hoax number
if (pf[0] == n)
return false;
// Finding sum of digits of distinct
// prime factors of the number n
int all_pf_sum = 0;
for (int i = 0; i < pf.Count; i++)
{
// Finding sum of digits in current
// prime factor pf[i].
int pf_sum;
for (pf_sum = 0; pf[i] > 0;
pf_sum += pf[i] % 10, pf[i] /= 10);
all_pf_sum += pf_sum;
}
// Finding sum of digits of number n
int sum_n;
for (sum_n = 0; n > 0; sum_n += n % 10,
n /= 10)
;
// Comparing the two calculated sums
return sum_n == all_pf_sum;
}
// Driver Code
public static void Main()
{
int n = 84;
if (isHoax(n))
Console.Write( "A Hoax Number\n");
else
Console.Write("Not a Hoax Number\n");
}
}
// This code is contributed by
// Manish Shaw (manishshaw1)
JavaScript
<script>
// Javascript code to check if a number is a hoax
// number or not.
// Function to find distinct prime factors
// of given number n
function primeFactors(n)
{
var res =[];
if (n % 2 == 0) {
while (n % 2 == 0)
n = parseInt(n / 2);
res.push(2);
}
// n is odd at this point, since it is no
// longer divisible by 2. So we can test
// only for the odd numbers, whether they
// are factors of n
for (var i = 3; i <= Math.sqrt(n); i = i + 2) {
// Check if i is prime factor
if (n % i == 0) {
while (n % i == 0)
n = parseInt(n / i);
res.push(i);
}
}
// This condition is to handle the case
// when n is a prime number greater than 2
if (n > 2)
res.push(n);
return res;
}
// Function to calculate sum of digits of
// distinct prime factors of given number n
// and sum of digits of number n and compare
// the sums obtained
function isHoax(n)
{
// Distinct prime factors of n are being
// stored in vector pf
var pf = primeFactors(n);
// If n is a prime number, it cannot be a
// hoax number
if (pf[0] == n)
return false;
// Finding sum of digits of distinct prime
// factors of the number n
var all_pf_sum = 0;
for (var i = 0; i < pf.length; i++) {
// Finding sum of digits in current
// prime factor pf[i].
var pf_sum;
for (pf_sum = 0; pf[i] > 0;
pf_sum += pf[i] % 10, pf[i] = parseInt(pf[i]/10))
;
all_pf_sum += pf_sum;
}
// Finding sum of digits of number n
var sum_n;
for (sum_n = 0; n > 0; sum_n += n % 10,
n = parseInt(n/10))
;
// Comparing the two calculated sums
return sum_n == all_pf_sum;
}
// Driver Method
var n = 84;
if (isHoax(n))
document.write( "A Hoax Number");
else
document.write( "Not a Hoax Number");
// This code is contributed by rrrtnx.
</script>
PHP
<?php
// PHP code to check if a number
// is a hoax number or not.
// Function to find distinct prime
// factors of given number n
function primeFactors($n)
{
$res = array();
if ($n % 2 == 0)
{
while ($n % 2 == 0)
$n = (int)$n / 2;
array_push($res, 2);
}
// n is odd at this point, since
// it is no longer divisible by 2.
// So we can test only for the odd
// numbers, whether they are factors of n
for ($i = 3; $i <= sqrt($n); $i = $i + 2)
{
// Check if i is prime factor
if ($n % $i == 0)
{
while ($n % $i == 0)
$n = (int)$n / $i;
array_push($res, $i);
}
}
// This condition is to handle
// the case when n is a prime
// number greater than 2
if ($n > 2)
array_push($res, $n);
return $res;
}
// Function to calculate sum
// of digits of distinct prime
// factors of given number n
// and sum of digits of number
// n and compare the sums obtained
function isHoax($n)
{
// Distinct prime factors
// of n are being stored
// in vector pf
$pf = primeFactors($n);
// If n is a prime number,
// it cannot be a hoax number
if ($pf[0] == $n)
return false;
// Finding sum of digits of distinct
// prime factors of the number n
$all_pf_sum = 0;
for ($i = 0; $i < count($pf); $i++)
{
// Finding sum of digits in
// current prime factor pf[i].
$pf_sum;
for ($pf_sum = 0; $pf[$i] > 0;
$pf_sum += $pf[$i] % 10,
$pf[$i] /= 10)
;
$all_pf_sum += $pf_sum;
}
// Finding sum of digits of number n
for ($sum_n = 0; $n > 0;
$sum_n += $n % 10, $n /= 10)
;
// Comparing the two calculated sums
return $sum_n == $all_pf_sum;
}
// Driver Code
$n = 84;
if (isHoax($n))
echo ("A Hoax Number\n");
else
echo ("Not a Hoax Number\n");
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
Output :
A Hoax Number
Time Complexity: O(?n log n)
Auxiliary Space: O(n)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem