DEV Community

dev.to staff
dev.to staff

Posted on

13

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!

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (21)

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

Collapse
 
emadsaber profile image
emadsaber

JavaScript

function isAutomorphic(x){
    return `${x * x}`.endsWith(`${x}`);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
emrivero profile image
Emilio Martínez Rivero

Javascript

const automorphic = n => ((n ** 2) - n) % 10 === 0
Collapse
 
craigmc08 profile image
Craig McIlwrath

This doesn't work. For 11, (11² - 11) % 10 = (121 - 11) % 10 = 110 % 10 = 0. Your function would return true, but 11 is not automorphic.

Redis image

Short-term memory for faster
AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay