DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #183 - Automorphic Numbers

Setup

For this challenge, implement a function that will return true if a number is Automorphic. Automorphics are numbers whose square ends in the same digits as the number itself. The number will always be positive.

Examples

autoMorphic(13) => false
13 squared is 69. Since 69 does not end in 13, return false.

autoMorphic(25) => true
25 squared is 625. Since 625 ends in 25, return true.

Tests

autoMorphic(6)

autoMorphic(625)

autoMorphic(225)

Good luck!~


This challenge comes from MrZizoScream on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (21)

Collapse
 
natonathan profile image
Nathan Tamez

Here is some crappy Java Solution


import commonHelpers.IOHelpers;


public class Main {
    public static void main(String[] args){
        IOHelpers.print("1: is 25 Automorphic? "+ autoMorphic(25));
        IOHelpers.print("2: is 13 Automorphic? "+ autoMorphic(13));
        IOHelpers.print("3: is 6 Automorphic? "+ autoMorphic(6));
        IOHelpers.print("4: is 625 Automorphic? "+ autoMorphic(625));
        IOHelpers.print("5: is 255 Automorphic? "+ autoMorphic(255));
    }

    private static boolean autoMorphic(double number){
        String numberString = String.valueOf(number);
        String numberSquareString = String.valueOf(Math.pow(number, 2));
        return numberSquareString.endsWith(numberString);
    }
}

Here is the output

1: is 25 Automorphic? true
2: is 13 Automorphic? false
3: is 6 Automorphic? true
4: is 625 Automorphic? true
5: is 255 Automorphic? false
Collapse
 
craigmc08 profile image
Craig McIlwrath

Solution without strings (Haskell)

automorphic :: (Integral a) => a -> Bool
automorphic n = (n*n - n) `rem` (10 ^ digits) == 0
  where digits = (+1) $ floor $ logBase 10 $ fromIntegral n

An explanation: if n² ends in n, then n² - n ends in as many zeroes as there are digits in n. I'm not going to prove this here, I believe it is pretty straightforward. If a number ends in m zeroes, then the number is divisible (remainder of 0) by 10m.

The test now is: is n² - n divisible by 10 ^ (number of digits in n). Finding the number of digits is simple: the log base 10 of a number with m digits is m - x, x is in the interval (0,1]. This translates to floor(log base 10 of n) + 1 = number of digits.

So that's my solution.

Collapse
 
aminnairi profile image
Amin

Elm

square : Float -> Float
square float =
    float * float


isAutomorphic : Int -> Bool
isAutomorphic integer =
    integer
        |> toFloat
        |> square
        |> String.fromFloat
        |> String.endsWith (String.fromInt integer)
Collapse
 
exts profile image
Lamonte • Edited

dart

import 'dart:math';
bool autoMorphic(int number) {
  return pow(number, 2).toString().endsWith(number.toString());
}

//print(autoMorphic(13)); //false
//print(autoMorphic(25)); //true
//print(autoMorphic(6)); //true
//print(autoMorphic(625)); //true
//print(autoMorphic(225)); //false

bool autoMorphic2(int number) {
  return number != 0 
    ? pow(number, 2).toString().endsWith(number.abs().toString())
    : true;
}

//print(autoMorphic2(0)); //true
//print(autoMorphic2(-25)); //true
Collapse
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑

Python

def auto_morphic(num):
  return str(num*num)[-1] == str(num)[-1]
Collapse
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑

More tests:

print(auto_morphic(13), False)
print(auto_morphic(25), True)
print(auto_morphic(6), True)
print(auto_morphic(625), True)
print(auto_morphic(225), True)
print(auto_morphic(1), True)
print(auto_morphic(2), False)
print(auto_morphic(3), False)
print(auto_morphic(4), False)
print(auto_morphic(5), True)
print(auto_morphic(6), True)
print(auto_morphic(7), False)
print(auto_morphic(8), False)
print(auto_morphic(9), False)
print(auto_morphic(10), True)
print(auto_morphic(11), True)
print(auto_morphic(12), False)
Collapse
 
j_saikiranreddy profile image
J.SaiKiranReddy

11 should be false?

Thread Thread
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑

11 * 11 is 121, which ends in 1 hence return True.

Thread Thread
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑

oops - i interpreted the problem incorrectly as only the one’s digits needing to match.

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli • Edited

No regex for this one :(

function autoMorphic(n)
{
  const nsq = n*n;
  const nthpower = Math.floor(Math.log10(n))+1;
  const bigbit = Math.floor(nsq / (10**nthpower)) * (10**nthpower);
  const smallbit = nsq-bigbit;
  return smallbit == n;
}
Collapse
 
cipharius profile image
Valts Liepiņš

Ruby

def autoMorphic num
    dig = num.digits
    (num**2).digits[...dig.length].eql? dig
end

and Haskell:

autoMorphic :: Int -> Bool
autoMorphic x = (chars ==) . take (length chars) . reverse . show $ x^2
    where chars = reverse $ show x
Collapse
 
emadsaber profile image
emadsaber

JavaScript

function isAutomorphic(x){
    return `${x * x}`.endsWith(`${x}`);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
savagepixie profile image
SavagePixie

I think this should do it in JavaScript:

const isAutomorphic = n => (n ** 2).toString().endsWith(n.toString())
Collapse
 
divyanshpratap profile image
divyansh-pratap • Edited

include

int main()
{
int a , b , q , s=0 ,i=0 , r;
printf("enter the number");
scanf("%d" , &a);
b=a*a;
q=a;

// calculating number of digits in a //

while(q!=0)
{
q=q/10;
i++;

}
q=b;

//finding last two digits of b in reverse order //
for(i;i>0;i--)
{
r=q%10;
s=s*10+r;
q=q/10;
}
q=s;
s=0;

//finding digits in correct order //

while(q!=0)
{
    r=q%10;
    s=s*10+r;
    q=q/10;
}

// now comparing with input //

if(s==a)
{
    printf("true\n");
}
else
{
    printf("false");
}
return 0;

}

This is the solution in c.

steps :-
1 . ask for a input.
2 . calculate and store the square in another variable.
3 . calculate the number of digits in the input.
4 . find the last two digit of square number .
5 . compare the last two digit number with the input number and give the result.

If anyone is intrested in c programming please drop a comment. we can grow together .

output :