📜  检查号码是否为特洛伊木马号码

📅  最后修改于: 2021-04-29 14:48:43             🧑  作者: Mango

给定一个数字N 。任务是检查N是否为特洛伊木马编号。

Trojan Number是一个数字,它是一个强数,但不是一个完美的幂。如果对于每个主除数或N的因子p,p2也是一个除数,则数字N被称为强数。换句话说,每个素因数至少出现两次。

所有特洛伊木马数字都很强。但是,并非所有强数都是Trojan数:只有那些不能表示为m k的数字,其中m和k是大于1的正整数。

例子

Input : N = 108
Output : YES

Input : N = 8
Output : NO

这个想法是存储每个素数的计数,并检查该计数是否大于2,那么它将是一个强数

该部分可以通过筛的质因数分解很容易地计算出来。

下一步是检查给定的数字是否不能表示为x y 。要检查数字是否是完美的幂,请参阅本文。

下面是上述问题的实现:

C++
// CPP program to check if a number is
// Trojan Number or not
  
#include 
using namespace std;
  
// Function to check if a number
// can be expressed as x^y
bool isPerfectPower(int n)
{
    if (n == 1)
        return true;
  
    // Try all numbers from 2 to sqrt(n) as base
    for (int x = 2; x <= sqrt(n); x++) {
        int y = 2;
        int p = pow(x, y);
  
        // Keep increasing y while power 'p'
        // is smaller than n.
        while (p <= n && p > 0) {
            if (p == n)
                return true;
            y++;
            p = pow(x, y);
        }
    }
    return false;
}
  
// Function to check if a number is Strong
bool isStrongNumber(int n)
{
    unordered_map count;
    while (n % 2 == 0) {
        n = n / 2;
        count[2]++;
    }
  
    // count the number for each prime factor
    for (int i = 3; i <= sqrt(n); i += 2) {
        while (n % i == 0) {
            n = n / i;
            count[i]++;
        }
    }
  
    if (n > 2)
        count[n]++;
  
    int flag = 0;
  
    for (auto b : count) {
  
        // minimum number of prime divisors
        // should be 2
        if (b.second == 1) {
            flag = 1;
            break;
        }
    }
  
    if (flag == 1)
        return false;
    else
        return true;
}
  
// Function to check if a number
// is Trojan Number
bool isTrojan(int n)
{
    if (!isPerfectPower(n) && isStrongNumber(n))
        return true;
    else
        return false;
}
  
// Driver Code
int main()
{
    int n = 108;
  
    if (isTrojan(n))
        cout << "YES";
    else
        cout << "NO";
  
    return 0;
}


Java
// Java program to check if a number is
// Trojan Number or not
import java.util.*;
  
class GFG 
{
  
    // Function to check if a number
    // can be expressed as x^y
    static boolean isPerfectPower(int n)
    {
        if (n == 1)
        {
            return true;
        }
  
        // Try all numbers from 2 to sqrt(n) as base
        for (int x = 2; x <= Math.sqrt(n); x++) 
        {
            int y = 2;
            int p = (int) Math.pow(x, y);
  
            // Keep increasing y while power 'p'
            // is smaller than n.
            while (p <= n && p > 0) 
            {
                if (p == n) 
                {
                    return true;
                }
                y++;
                p = (int) Math.pow(x, y);
            }
        }
        return false;
    }
  
    // Function to check if a number is Strong
    static boolean isStrongNumber(int n) 
    {
        HashMap count = new HashMap();
        while (n % 2 == 0) 
        {
            n = n / 2;
            if (count.containsKey(2)) 
            {
                count.put(2, count.get(2) + 1);
            } 
            else 
            {
                count.put(2, 1);
            }
        }
  
        // count the number for each prime factor
        for (int i = 3; i <= Math.sqrt(n); i += 2) 
        {
            while (n % i == 0)
            {
                n = n / i;
                if (count.containsKey(i))
                {
                    count.put(i, count.get(i) + 1);
                }
                else 
                {
                    count.put(i, 1);
                }
            }
        }
  
        if (n > 2)
        {
            if (count.containsKey(n))
            {
                count.put(n, count.get(n) + 1);
            } 
            else 
            {
                count.put(n, 1);
            }
        }
  
        int flag = 0;
  
        for (Map.Entry b : count.entrySet()) 
        {
  
            // minimum number of prime divisors
            // should be 2
            if (b.getValue() == 1)
            {
                flag = 1;
                break;
            }
        }
  
        if (flag == 1) 
        {
            return false;
        } 
        else 
        {
            return true;
        }
    }
  
    // Function to check if a number
    // is Trojan Number
    static boolean isTrojan(int n) 
    {
        if (!isPerfectPower(n) && isStrongNumber(n))
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 108;
  
        if (isTrojan(n)) 
        {
            System.out.println("Yes");
        } 
        else 
        {
            System.out.println("No");
        }
    }
} 
  
// This code is contributed by PrinciRaj1992


Python3
# Python 3 program to check if a number 
# is Trojan Number or not
from math import sqrt, pow
  
# Function to check if a number
# can be expressed as x^y
def isPerfectPower(n):
    if n == 1:
        return True
  
    # Try all numbers from 2 to 
    # sqrt(n) as base
    for x in range(2, int(sqrt(n)) + 1):
        y = 2
        p = pow(x, y)
  
        # Keep increasing y while power 
        # 'p' is smaller than n.
        while p <= n and p > 0:
            if p == n:
                return True
            y += 1
            p = pow(x, y)
  
    return False
  
# Function to check if a number 
# is Strong
def isStrongNumber(n):
    count = {i:0 for i in range(n)}
    while n % 2 == 0:
        n = n // 2
        count[2] += 1
  
    # count the number for each
    # prime factor
    for i in range(3,int(sqrt(n)) + 1, 2):
        while n % i == 0:
            n = n // i
            count[i] += 1
  
    if n > 2:
        count[n] += 1
  
    flag = 0
  
    for key,value in count.items():
          
        # minimum number of prime 
        # divisors should be 2
        if value == 1:
            flag = 1
            break
      
    if flag == 1:
        return False
    return True
  
# Function to check if a number
# is Trojan Number
def isTrojan(n):
    return isPerfectPower(n) == False and isStrongNumber(n)
      
# Driver Code
if __name__ == '__main__':
    n = 108
  
    if (isTrojan(n)):
        print("YES")
    else:
        print("NO")
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to check if a number is
// Trojan Number or not
using System;
using System.Collections.Generic;
      
class GFG 
{
  
    // Function to check if a number
    // can be expressed as x^y
    static bool isPerfectPower(int n)
    {
        if (n == 1)
        {
            return true;
        }
  
        // Try all numbers from 2 to sqrt(n) as base
        for (int x = 2; x <= Math.Sqrt(n); x++) 
        {
            int y = 2;
            int p = (int) Math.Pow(x, y);
  
            // Keep increasing y while power 'p'
            // is smaller than n.
            while (p <= n && p > 0) 
            {
                if (p == n) 
                {
                    return true;
                }
                y++;
                p = (int) Math.Pow(x, y);
            }
        }
        return false;
    }
  
    // Function to check if a number is Strong
    static bool isStrongNumber(int n) 
    {
        Dictionary count = new Dictionary();
        while (n % 2 == 0) 
        {
            n = n / 2;
            if (count.ContainsKey(2)) 
            {
                count[2] = count[2] + 1;
            } 
            else
            {
                count.Add(2, 1);
            }
        }
  
        // count the number for each prime factor
        for (int i = 3; i <= Math.Sqrt(n); i += 2) 
        {
            while (n % i == 0)
            {
                n = n / i;
                if (count.ContainsKey(i))
                {
                    count[i] = count[i] + 1;
                }
                else
                {
                    count.Add(i, 1);
                }
            }
        }
  
        if (n > 2)
        {
            if (count.ContainsKey(n))
            {
                count[n] = count[n] + 1;
            } 
            else
            {
                count.Add(n, 1);
            }
        }
  
        int flag = 0;
  
        foreach(KeyValuePair b in count)
        {
  
            // minimum number of prime divisors
            // should be 2
            if (b.Value == 1)
            {
                flag = 1;
                break;
            }
        }
  
        if (flag == 1) 
        {
            return false;
        } 
        else
        {
            return true;
        }
    }
  
    // Function to check if a number
    // is Trojan Number
    static bool isTrojan(int n) 
    {
        if (!isPerfectPower(n) && 
             isStrongNumber(n))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 108;
  
        if (isTrojan(n)) 
        {
            Console.WriteLine("Yes");
        } 
        else
        {
            Console.WriteLine("No");
        }
    }
}
  
// This code is contributed by Princi Singh


输出:
YES