Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Canada Numbers

  • Last Updated : 16 Jul, 2021

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… 
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



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




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

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




<script>
// Javascript implementation for the
// above approach
 
 
    // Function to calculate sum of
    // all trivial divisors
    // of given natural number
    function divSum( num) {
        // Final result of summation
        // of trivial divisors
        let result = 0;
 
        // Find all divisors which
        // divides 'num'
        for (let 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
    function getSum( n) {
        let sum = 0;
        while (n != 0) {
            let r = n % 10;
            sum = sum + r * r;
            n = parseInt(n / 10);
        }
        return sum;
    }
 
    // Function to check if N is a
    // Canada number
    function isCanada( n) {
        return divSum(n) == getSum(n);
    }
 
    // Driver Code
      
        // Given Number
        let n = 125;
 
        // Function Call
        if (isCanada(n))
            document.write("Yes");
        else
            document.write("No");
 
// This code contributed by Rajput-Ji
 
</script>
Output: 
Yes

 

Time Complexity: O(n1/2)

Auxiliary Space: O(1)

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :