📜  朗达编号

📅  最后修改于: 2021-04-26 19:24:23             🧑  作者: Mango

给定整数N ,任务是检查N是否为以10为底的Rhonda数字。

例子:

方法:这个想法是找到N的所有素数之和,并检查N的素数之和的10倍是否等于N的数位乘积。

下面是上述方法的实现:

C++
// C++ implementation to check if N
// is a Rhonda number
 
#include 
using namespace std;
 
// Function to find the 
// product of digits
int getProduct(int n)
{
    int product = 1;
 
    while (n != 0) {
        product = product * (n % 10);
        n = n / 10;
    }
    return product;
}
 
// Function to find sum of all prime
// factors of a given number N
int sumOfprimeFactors(int n)
{
    int sum = 0;
    // add the number of
    // 2s that divide n
    while (n % 2 == 0) {
        sum += 2;
        n = n / 2;
    }
 
    // N must be odd at this
    // point. So we can skip
    // one element
    for (int i = 3; i <= sqrt(n);
                      i = i + 2) {
         
        // While i divides n,
        // add i and divide n
        while (n % i == 0) {
            sum += i;
            n = n / i;
        }
    }
 
    // Condition to handle the case when N
    // is a prime number greater than 2
    if (n > 2)
        sum += n;
    return sum;
}
 
// Function to check if n
// is Rhonda number
bool isRhondaNum(int N)
{
    return 10 * sumOfprimeFactors(N) == getProduct(N);
}
 
// Driver code
int main()
{
    int n = 1568;
    if (isRhondaNum(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to check if N
// is a Rhonda number
import java.util.*;
import java.lang.*;
// import Math;
 
class GFG{
 
// Function to find the
// product of digits
static int getProduct(int n)
{
    int product = 1;
 
    while (n != 0)
    {
        product = product * (n % 10);
        n = n / 10;
    }
    return product;
}
 
// Function to find sum of all prime
// factors of a given number N
static int sumOfprimeFactors(int n)
{
    int sum = 0;
     
    // Add the number of
    // 2s that divide n
    while (n % 2 == 0)
    {
        sum += 2;
        n = n / 2;
    }
 
    // N must be odd at this
    // point. So we can skip
    // one element
    for(int i = 3; i <= Math.sqrt(n);
            i = i + 2)
    {
         
        // While i divides n,
        // add i and divide n
        while (n % i == 0)
        {
            sum += i;
            n = n / i;
        }
    }
     
    // Condition to handle the case when N
    // is a prime number greater than 2
    if (n > 2)
        sum += n;
         
    return sum;
}
 
// Function to check if n
// is Rhonda number
static boolean isRhondaNum(int N)
{
    return (10 * sumOfprimeFactors(N) ==
                        getProduct(N));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Number n
    int n = 1568;
    if (isRhondaNum(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by vikas_g


Python3
# Python3 implementation to check
# if N is a Rhonda number
import math
 
# Function to find the
# product of digits
def getProduct(n):
     
    product = 1
 
    while (n != 0):
        product = product * (n % 10)
        n = n // 10
     
    return product
 
# Function to find sum of all prime
# factors of a given number N
def sumOfprimeFactors(n):
     
    Sum = 0
     
    # Add the number of
    # 2s that divide n
    while (n % 2 == 0):
        Sum += 2
        n = n // 2
 
    # N must be odd at this
    # point. So we can skip
    # one element
    for i in range(3, int(math.sqrt(n)) + 1, 2):
         
        # While i divides n,
        # add i and divide n
        while (n % i == 0):
            Sum += i
            n = n // i
 
    # Condition to handle the case when N
    # is a prime number greater than 2
    if (n > 2):
        Sum += n
         
    return Sum
 
# Function to check if n
# is Rhonda number
def isRhondaNum(N):
     
    return bool(10 * sumOfprimeFactors(N) ==
                            getProduct(N))
     
# Driver code
n = 1568
 
if (isRhondaNum(n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyeshrabadiya07


C#
// C# implementation to check if N
// is a Rhonda number
using System;
 
class GFG{
 
// Function to find the
// product of digits
static int getProduct(int n)
{
    int product = 1;
 
    while (n != 0)
    {
        product = product * (n % 10);
        n = n / 10;
    }
    return product;
}
 
// Function to find sum of all prime
// factors of a given number N
static int sumOfprimeFactors(int n)
{
    int sum = 0;
     
    // Add the number of
    // 2s that divide n
    while (n % 2 == 0)
    {
        sum += 2;
        n = n / 2;
    }
 
    // N must be odd at this
    // point. So we can skip
    // one element
    for(int i = 3; i <= Math.Sqrt(n);
            i = i + 2)
    {
         
        // While i divides n,
        // add i and divide n
        while (n % i == 0)
        {
            sum += i;
            n = n / i;
        }
    }
     
    // Condition to handle the case when N
    // is a prime number greater than 2
    if (n > 2)
        sum += n;
         
    return sum;
}
 
// Function to check if n
// is Rhonda number
static bool isRhondaNum(int N)
{
    return (10 * sumOfprimeFactors(N) ==
                        getProduct(N));
}
 
// Driver code
public static void Main(String []args)
{
    int n = 1568;
         
    if (isRhondaNum(n))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by vikas_g


Javascript


输出:
Yes

参考文献: OEIS