Rhonda numbers
Last Updated :
24 Nov, 2021
Given an integer N, the task is to check if N is a Rhonda numbers to base 10.
Rhonda numbers to base 10 are numbers if the product of its digits equals 10*Sum of prime factors of N (including multiplicity).
Examples:
Input: N = 1568
Output: Yes
Explanation:
1568’s prime factorization = 25 * 72.
Sum of prime factors = 2*5+7*2=24.
Product of digits of 1568 = 1*5*6*8=240 = 10*24.
Hence 1568 is a Rhonda number to base 10.
Input: N = 28
Output: No
Approach: The idea is to find the sum of all prime factors of N and check if ten times of Sum of prime factors of N is equal to the product of digits of N or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getProduct( int n)
{
int product = 1;
while (n != 0) {
product = product * (n % 10);
n = n / 10;
}
return product;
}
int sumOfprimeFactors( int n)
{
int sum = 0;
while (n % 2 == 0) {
sum += 2;
n = n / 2;
}
for ( int i = 3; i <= sqrt (n);
i = i + 2) {
while (n % i == 0) {
sum += i;
n = n / i;
}
}
if (n > 2)
sum += n;
return sum;
}
bool isRhondaNum( int N)
{
return 10 * sumOfprimeFactors(N) == getProduct(N);
}
int main()
{
int n = 1568;
if (isRhondaNum(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int getProduct( int n)
{
int product = 1 ;
while (n != 0 )
{
product = product * (n % 10 );
n = n / 10 ;
}
return product;
}
static int sumOfprimeFactors( int n)
{
int sum = 0 ;
while (n % 2 == 0 )
{
sum += 2 ;
n = n / 2 ;
}
for ( int i = 3 ; i <= Math.sqrt(n);
i = i + 2 )
{
while (n % i == 0 )
{
sum += i;
n = n / i;
}
}
if (n > 2 )
sum += n;
return sum;
}
static boolean isRhondaNum( int N)
{
return ( 10 * sumOfprimeFactors(N) ==
getProduct(N));
}
public static void main(String[] args)
{
int n = 1568 ;
if (isRhondaNum(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def getProduct(n):
product = 1
while (n ! = 0 ):
product = product * (n % 10 )
n = n / / 10
return product
def sumOfprimeFactors(n):
Sum = 0
while (n % 2 = = 0 ):
Sum + = 2
n = n / / 2
for i in range ( 3 , int (math.sqrt(n)) + 1 , 2 ):
while (n % i = = 0 ):
Sum + = i
n = n / / i
if (n > 2 ):
Sum + = n
return Sum
def isRhondaNum(N):
return bool ( 10 * sumOfprimeFactors(N) = =
getProduct(N))
n = 1568
if (isRhondaNum(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int getProduct( int n)
{
int product = 1;
while (n != 0)
{
product = product * (n % 10);
n = n / 10;
}
return product;
}
static int sumOfprimeFactors( int n)
{
int sum = 0;
while (n % 2 == 0)
{
sum += 2;
n = n / 2;
}
for ( int i = 3; i <= Math.Sqrt(n);
i = i + 2)
{
while (n % i == 0)
{
sum += i;
n = n / i;
}
}
if (n > 2)
sum += n;
return sum;
}
static bool isRhondaNum( int N)
{
return (10 * sumOfprimeFactors(N) ==
getProduct(N));
}
public static void Main(String []args)
{
int n = 1568;
if (isRhondaNum(n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function getProduct( n)
{
let product = 1;
while (n != 0)
{
product = product * (n % 10);
n = parseInt(n / 10);
}
return product;
}
function sumOfprimeFactors( n) {
let sum = 0;
while (n % 2 == 0) {
sum += 2;
n = parseInt(n / 2);
}
for ( let i = 3; i <= Math.sqrt(n); i = i + 2)
{
while (n % i == 0)
{
sum += i;
n = parseInt(n / i);
}
}
if (n > 2)
sum += n;
return sum;
}
function isRhondaNum( N) {
return (10 * sumOfprimeFactors(N) == getProduct(N));
}
let n = 1568;
if (isRhondaNum(n)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Time Complexity: O(sqrt(N))
References: OEIS
Share your thoughts in the comments
Please Login to comment...