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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

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
 
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
 
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
 
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
 
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
 
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
 
savagepixie profile image
SavagePixie β€’

I think this should do it in JavaScript:

const isAutomorphic = n => (n ** 2).toString().endsWith(n.toString())
Collapse
 
lakatos88 profile image
Alex Lakatos πŸ₯‘ β€’ β€’ Edited

JavaScript


let autoMorphic = (x) => {
  x = parseInt(x, 10);
  return (x*x).toString().indexOf(x) === ((x*x).toString().length - x.toString().length);
}

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay