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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,683 questions

55,437 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
...