给定数字N ,任务是检查N是否为不可触摸数字。如果N是不可触摸的数字,则打印“是”,否则打印“否” 。
Untouchable Number are numbers which are not the sum of the proper divisors of any number.
例子:
Input: N = 5
Output: Yes
Input: N = 20
Output: No
方法:这个想法是找到数字N的适当除数的总和,并检查总和是否等于N。如果sum不等于N ,则N是不可触摸的数字,然后打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate sum of
// all proper divisors of num
int divSum(int num)
{
// Final result of summation
// of divisors
int result = 0;
// Find all divisors of num
for (int i = 2; i <= sqrt(num); i++) {
// If 'i' is divisor of 'num'
if (num % i == 0) {
// If both divisors are same
// then add it only once else
// add both
if (i == (num / i))
result += i;
else
result += (i + num / i);
}
}
// Add 1 to the result as
// 1 is also a divisor
return (result + 1);
}
// Function to check if N is a
// Untouchable Number
bool isUntouchable(int n)
{
for (int i = 1; i <= 2 * n; i++) {
if (divSum(i) == n)
return false;
}
return true;
}
// Driver Code
int main()
{
// Given Number N
int N = 52;
// Function Call
if (isUntouchable(n))
cout << "Yes";
else
cout << "No";
}
Java
// Java program for the above approach
class GFG{
// Function to calculate sum of
// all proper divisors of num
static int divSum(int num)
{
// Final result of summation
// of divisors
int result = 0;
// Find all divisors of num
for(int i = 2; i <= Math.sqrt(num); i++)
{
// If 'i' is divisor of 'num'
if (num % i == 0)
{
// If both divisors are same
// then add it only once else
// add both
if (i == (num / i))
result += i;
else
result += (i + num / i);
}
}
// Add 1 to the result as
// 1 is also a divisor
return (result + 1);
}
// Function to check if N is a
// Untouchable Number
static boolean isUntouchable(int n)
{
for(int i = 1; i <= 2 * n; i++)
{
if (divSum(i) == n)
return false;
}
return true;
}
// Driver code
public static void main(String[] args)
{
// Given Number N
int n = 52;
// Function Call
if (isUntouchable(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Pratima Pandey
Python3
# Python3 program for the above approach
import math;
# Function to calculate sum of
# all proper divisors of num
def divSum(num):
# Final result of summation
# of divisors
result = 0;
# Find all divisors of num
for i in range(2, int(math.sqrt(num))):
# If 'i' is divisor of 'num'
if (num % i == 0):
# If both divisors are same
# then add it only once else
# add both
if (i == (num // i)):
result += i;
else:
result += (i + num // i);
# Add 1 to the result as
# 1 is also a divisor
return (result + 1);
# Function to check if N is a
# Untouchable Number
def isUntouchable(n):
for i in range(1, 2 * n):
if (divSum(i) == n):
return False;
return True;
# Driver Code
# Given Number N
N = 52;
# Function Call
if (isUntouchable(N)):
print("Yes");
else:
print("No");
# This code is contributed by Code_Mech
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate sum of
// all proper divisors of num
static int divSum(int num)
{
// Final result of summation
// of divisors
int result = 0;
// Find all divisors of num
for(int i = 2; i <= Math.Sqrt(num); i++)
{
// If 'i' is divisor of 'num'
if (num % i == 0)
{
// If both divisors are same
// then add it only once else
// add both
if (i == (num / i))
result += i;
else
result += (i + num / i);
}
}
// Add 1 to the result as
// 1 is also a divisor
return (result + 1);
}
// Function to check if N is a
// Untouchable Number
static bool isUntouchable(int n)
{
for(int i = 1; i <= 2 * n; i++)
{
if (divSum(i) == n)
return false;
}
return true;
}
// Driver code
public static void Main()
{
// Given Number N
int n = 52;
// Function Call
if (isUntouchable(n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Code_Mech
Javascript
输出:
Yes
时间复杂度: O(sqrt(N))