Check if a Number is Automorphic | Explanation & Code

Check if a Number is Automorphic | Explanation & Code

Check if a Number is Automorphic | Explanation & Code

Introduction

An Automorphic Number is a number whose square ends with the same digits as the number itself. In other words, a number N is Automorphic if the last digits of are the same as N.

Examples of Automorphic Numbers

  1. 5 → 52=255^2 = 2552=25 → Ends with 5
  2. 6 → 62=366^2 = 3662=36 → Ends with 6
  3. 76 → 762=577676^2 = 5776762=5776 → Ends with 76
  4. 25 → 252=62525^2 = 625252=625 → Ends with 25

Examples of Non-Automorphic Numbers

  1. 7 → 72=497^2 = 4972=49 → Does not end with 7
  2. 9 → 92=819^2 = 8192=81 → Does not end with 9

Input and Output Format

  • Input: A single integer N.
  • Output:
    • Print "Automorphic Number" if N is Automorphic.
    • Otherwise, print "Not an Automorphic Number".

Example 1:

Input:

CopyEdit25

Output:

javascriptCopyEditAutomorphic Number

Explanation:
252=62525^2 = 625252=625 → Ends with 25

Example 2:

Input:

CopyEdit12

Output:

mathematicaCopyEditNot an Automorphic Number

Explanation:
122=14412^2 = 144122=144 → Does not end with 12


Algorithm to Check Automorphic Number

  1. Read the number N from the user.
  2. Compute the square of N, i.e., .
  3. Check if the last digits of match N:
    • Convert both N and to strings.
    • Extract the last digits of equal to the length of N.
    • Compare them.
  4. Print the result accordingly.

Implementation in C

cCopyEdit#include <stdio.h>

// Function to check if a number is Automorphic
int isAutomorphic(int num) {
    int square = num * num;

    // Temporary variable to extract last digits
    int temp = num;
    while (temp > 0) {
        // If the last digits do not match, return false
        if (square % 10 != temp % 10) {
            return 0;
        }
        square /= 10;
        temp /= 10;
    }
    return 1;
}

int main() {
    int num;
    
    // Input the number
    printf("Enter a number: ");
    scanf("%d", &num);

    // Check if the number is Automorphic
    if (isAutomorphic(num)) {
        printf("Automorphic Number\n");
    } else {
        printf("Not an Automorphic Number\n");
    }

    return 0;
}

Explanation of the Code

  1. Function isAutomorphic(int num)
    • Computes the square of the number.
    • Uses a while loop to compare the last digits of num and square.
    • Extracts digits using modulus (% 10) and removes them using division (/ 10).
    • If all last digits match, the number is Automorphic.
  2. Main function
    • Takes user input.
    • Calls isAutomorphic() to check if the number is Automorphic.
    • Prints the result accordingly.

Time Complexity Analysis

  • Extracting digits takes O(log N) time.
  • Overall complexity is O(log N), which is efficient.

Test Cases

InputExpected Output
5Automorphic Number
6Automorphic Number
25Automorphic Number
76Automorphic Number
7Not an Automorphic Number
12Not an Automorphic Number

Edge Cases to Consider

  1. Single-digit numbers (like 5 and 6) should be handled correctly.
  2. Numbers ending with zero (10, 100, 1000) should be tested.
  3. Large numbers should be checked for performance issues.

Advantages and Disadvantages

Advantages:

  • Simple to understand and implement.
  • Useful in mathematical and number theory concepts.

Disadvantages:

  • Not widely applicable in real-world scenarios.
  • Computational limitations for very large numbers.

Conclusion

An Automorphic Number is a number whose square ends with the same digits as the number itself. The provided C program efficiently checks Automorphic numbers using modulus and division operations. By running test cases and optimizing the approach, we ensure correct and efficient execution.