Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check if a number is automorphic number in Ruby

1 Answer

0 votes
# An Automorphic number is a number whose square ends with the same digits 
# as the original number. E.g – 5 : 5 * 5 = 25 //ends with 5 
  
class Automorphic 
    def isAutomorphic(num) 
        s = num.to_s
     
        square = num * num
      
        last = square % (10 ** s.length)
     
        print num, " square = ", square, " "
      
        return last == num
    end
end
  
def main() 
    o = Automorphic.new()
      
    print o.isAutomorphic(25), "\n"
    print o.isAutomorphic(5), "\n"
    print o.isAutomorphic(76), "\n"
    print o.isAutomorphic(98), "\n"
    print o.isAutomorphic(376), "\n"
    print o.isAutomorphic(36), "\n"
end
  
main()
  
  
  
  
#
# run:
# 
# 25 square = 625 true
# 5 square = 25 true
# 76 square = 5776 true
# 98 square = 9604 false
# 376 square = 141376 true
# 36 square = 1296 false
#

 



answered Oct 16, 2021 by avibootz
edited Oct 16, 2021 by avibootz

Related questions

1 answer 154 views
1 answer 268 views
1 answer 143 views
4 answers 302 views
1 answer 192 views
1 answer 161 views
161 views asked Dec 21, 2020 by avibootz
2 answers 233 views
233 views asked Dec 21, 2020 by avibootz
...