Monday, November 18, 2024
Google search engine

Tcefrep Numbers

Tcefrep Number a number N such that reverse(n) = sum of the proper divisors of N.
 

6, 498906, 20671542, 41673714…. 
 

 

Check if N is a Tcefrep number

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

Input: N = 498906 
Output: Yes 
Explanation: 
proper divisors of 498906 are 1, 2, 3, 6, 9, 18, 27, 54, 
9239, 18478, 27717, 55434, 83151, 166302, 249453, 
which sum to 609894, the reverse of 498906
Input: N = 120 
Output: No 
 

Approach: 
 

  1. We will find the sum of proper divisors of N
  2. We will find the reverse of N
  3. Then we will check if sum of proper divisors of N is equal to reverse of N or not, if equal then print “Yes” else print “No”.

Below is the implementation of the above approach:
 

C++




// C++ implementation to check if N
// is a Tcefrep number
#include <bits/stdc++.h>
using namespace std;
 
// Iterative function to
// reverse digits of num
int reverse(int num)
{
    int rev_num = 0;
    while(num > 0)
    {
        rev_num = rev_num*10 + num%10;
        num = num/10;
    }
    return rev_num;
}
 
// Function to calculate sum of
// all proper divisors
// num --> given natural number
int properDivSum(int num)
{
    // Final result of summation of divisors
    int result = 0;
   
    // find all divisors which divides '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);
}
 
bool isTcefrep(int n)
{
    return properDivSum(n) == reverse(n);
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 6;
 
    // Function Call
    if (isTcefrep(N))
        cout << "Yes";
    else
        cout << "No";
    return 0; 
     
}


Java




// Java program for above approach
class GFG{
 
// Iterative function to
// reverse digits of num
static int reverse(int num)
{
    int rev_num = 0;
    while(num > 0)
    {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
 
// Function to calculate sum of
// all proper divisors
// num --> given natural number
static int properDivSum(int num)
{
    // Final result of summation of divisors
    int result = 0;
     
    // find all divisors which divides '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);
}
 
static boolean isTcefrep(int n)
{
    return properDivSum(n) == reverse(n);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 6;
 
    // Function Call
    if (isTcefrep(N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Pratima Pandey


Python3




# Python3 implementation to check if N
# is a Tcefrep number
import math
 
# Iterative function to
# reverse digits of num
def reverse(num):
    rev_num = 0
    while(num > 0):
        rev_num = rev_num * 10 + num % 10
        num = num // 10
 
    return rev_num
 
# Function to calculate sum of
# all proper divisors
# num --> given natural number
def properDivSum(num):
     
    # Final result of summation of divisors
    result = 0
 
    # find all divisors which divides 'num'
    for i in range(2, (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)
 
    # Add 1 to the result as 1 is also a divisor
    return (result + 1)
 
def isTcefrep(n):
    return properDivSum(n) == reverse(n);
 
# Driver Code
 
# Given Number N
N = 6
 
# Function Call
if(isTcefrep(N)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Sanjit Prasad


C#




// C# program for above approach
using System;
class GFG{
 
// Iterative function to
// reverse digits of num
static int reverse(int num)
{
    int rev_num = 0;
    while(num > 0)
    {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
 
// Function to calculate sum of
// all proper divisors
// num --> given natural number
static int properDivSum(int num)
{
    // Final result of summation of divisors
    int result = 0;
     
    // find all divisors which divides '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);
}
 
static bool isTcefrep(int n)
{
    return properDivSum(n) == reverse(n);
}
 
// Driver Code
public static void Main()
{
    int N = 6;
 
    // Function Call
    if (isTcefrep(N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Nidhi_Biet


Javascript




<script>
 
// Javascript program for above approach
 
 
    // Iterative function to
    // reverse digits of num
    function reverse( num) {
        let rev_num = 0;
        while (num > 0) {
            rev_num = rev_num * 10 + num % 10;
            num = parseInt(num / 10);
        }
        return rev_num;
    }
 
    // Function to calculate sum of
    // all proper divisors
    // num --> given natural number
    function properDivSum( num) {
        // Final result of summation of divisors
        let result = 0;
 
        // find all divisors which divides 'num'
        for ( 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 isTcefrep( n) {
        return properDivSum(n) == reverse(n);
    }
 
    // Driver Code
      
        let N = 6;
 
        // Function Call
        if (isTcefrep(N))
            document.write("Yes");
        else
            document.write("No");
 
// This code contributed by gauravrajput1
 
</script>


Output: 

Yes

 

Time Complexity: O(n^2) 
Reference: http://www.numbersaplenty.com/set/tcefrep_number/
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
23 Mar, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments