加拿大号是一个数N,使得N的数字的平方之和等于N,即,非平凡除数的总和(N的约数之和) – 1 – N.
很少有加拿大的数字是:
125, 581, 8549, 16999…
检查N是否是加拿大号码
给定数字N ,任务是检查N是否是加拿大数字。如果N是加拿大号码,则打印“是”,否则打印“否” 。
例子:
Input: N = 125
Output: Yes
Explanation:
125’s factors are 1, 5, 25, 125
and 1^2 + 2^2 + 5^2 = 30 = 5 + 25.
Input: N = 16
Output: No
方法:这个想法是找到一个数字的所有适当除数的和,然后从中减去N和1。另外,找到N的数字平方和。现在检查两个和是否相同。如果所有适当除数的总和与N的位数的平方和相等,则该数字为加拿大数字。
下面是上述方法的实现:
C++
// C++ implementation for the
// above approach
#include
using namespace std;
// Function to calculate sum of
// all trivial divisors
// of given natural number
int divSum(int num)
{
// Final result of summation
// of trivial divisors
int result = 0;
// Find all divisors which
// divides 'num'
for (int i = 1; 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);
}
}
return (result - 1 - num);
}
// Function to return sum
// of squares
// of digits of N
int getSum(int n)
{
int sum = 0;
while (n != 0) {
int r = n % 10;
sum = sum + r * r;
n = n / 10;
}
return sum;
}
// Function to check if N is a
// Canada number
bool isCanada(int n)
{
return divSum(n) == getSum(n);
}
// Driver Code
int main()
{
// Given Number
int n = 125;
// Function Call
if (isCanada(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation for the
// above approach
import java.io.*;
class GFG{
// Function to calculate sum of
// all trivial divisors
// of given natural number
static int divSum(int num)
{
// Final result of summation
// of trivial divisors
int result = 0;
// Find all divisors which
// divides 'num'
for (int i = 1; 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);
}
}
return (result - 1 - num);
}
// Function to return sum
// of squares
// of digits of N
static int getSum(int n)
{
int sum = 0;
while (n != 0)
{
int r = n % 10;
sum = sum + r * r;
n = n / 10;
}
return sum;
}
// Function to check if N is a
// Canada number
static boolean isCanada(int n)
{
return divSum(n) == getSum(n);
}
// Driver Code
public static void main (String[] args)
{
// Given Number
int n = 125;
// Function Call
if (isCanada(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by shubhamsingh10
Python3
# Python3 implementation for the
# above approach
import math
# Function to calculate sum of
# all trivial divisors
# of given natural number
def divSum(num):
# Final result of summation
# of trivial divisors
result = 0
# Find all divisors which
# divides 'num'
for i in range(1, int(math.sqrt(num)) + 1):
# 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)
return (result - 1 - num)
# Function to return sum
# of squares
# of digits of N
def getSum(n):
sum = 0
while (n != 0):
r = n % 10
sum = sum + r * r
n = n // 10
return sum
# Function to check if N is a
# Canada number
def isCanada(n):
return divSum(n) == getSum(n)
# Driver Code
# Given Number
n = 125
# Function Call
if (isCanada(n)):
print('Yes')
else:
print('No')
# This code is contributed by Yatin
C#
// C# implementation for the
// above approach
using System;
class GFG{
// Function to calculate sum of
// all trivial divisors
// of given natural number
static int divSum(int num)
{
// Final result of summation
// of trivial divisors
int result = 0;
// Find all divisors which
// divides 'num'
for (int i = 1; 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);
}
}
return (result - 1 - num);
}
// Function to return sum
// of squares
// of digits of N
static int getSum(int n)
{
int sum = 0;
while (n != 0)
{
int r = n % 10;
sum = sum + r * r;
n = n / 10;
}
return sum;
}
// Function to check if N is a
// Canada number
static bool isCanada(int n)
{
return divSum(n) == getSum(n);
}
// Driver Code
public static void Main()
{
// Given Number
int n = 125;
// Function Call
if (isCanada(n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Code_Mech
Javascript
输出:
Yes
参考: http : //www.numbersaplenty.com/set/Canada_number/