Canada Numbers

Canada Number is a number N such that the sum of the squares of the digits of N is equal to the sum of the non-trivial divisors of N, i.e. (Sum of divisors of N)- 1 – N.

Few Canada numbers are:

125, 581, 8549, 16999…

Check if N is an Canada number

Given a number N, the task is to check if N is an Canada Number or not. If N is an Canada Number then print “Yes” else print “No”.

Examples:



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

Approach: The idea is to find Sum of all proper divisors of a number and subtract N and 1 from it. Also, find the sum of squares of digits of N. Now check if both the sum are the same or not. If the sum of all proper divisors and sum of squares of digits of N are equal then the number is a Canada number.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation for the 
// above approach
  
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes

Reference: http://www.numbersaplenty.com/set/Canada_number/

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.