Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:25:30+00:00 2026-06-04T12:25:30+00:00

i am trying to make a model-class to find the grates common division for

  • 0

i am trying to make a model-class to find the grates common division for 2 numbers. if i put 68 and 22 the answer is 2 and is correct but if i put 678 352 the results is 326 and is wrong! should be 2! any ideas??

def gcd 
  if firstnumber > secondnumber
    div= firstnumber/ secondnumber
    ip=firstnumber % secondnumber
    fi = div / ip
    ip
  else 
    i = firstnumber 
    s = secondnumber 
    secondnumber = firstnumber 
    firstnumber = s 
    div= firstnumber/ secondnumber 
    ip=firstnumber % secondnumber 
    fi = div / ip ip 
  end 
end

with loop

def gcd
   a = firstnumber
   b=secondnumber
   ip = 50
   if firstnumber > secondnumber
     begin   
       fi=a/b
       re=a%b    
       fi=b/re
       re=re%b
       ip=re        
     end until ip != 0
     return re  
   end
   else
     i = firstnumber
     s = secondnumber
     secondnumber = firstnumber
     firstnumber = s
     div= firstnumber/ secondnumber
     ip=firstnumber % secondnumber
     fi = div / ip
     ip
   end
end

$end

and the same thing happens

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:25:32+00:00Added an answer on June 4, 2026 at 12:25 pm

    Ruby has a Class Integer which has the method gcd. Don’t reinvent the wheel. Read here http://www.ruby-doc.org/core-1.9.3/Integer.html#method-i-gcd

    irb(main):001:0> 68.gcd(22)
    => 2
    irb(main):002:0> 678.gcd(352)
    => 2
    

    The source for that method is

       def gcd(n)
         m = self.abs
         n = n.abs
    
         return n if m == 0
         return m if n == 0
    
         b = 0
         while n[0] == 0 && m[0] == 0
           b += 1; n >>= 1; m >>= 1
         end
         m >>= 1 while m[0] == 0
         n >>= 1 while n[0] == 0
         while m != n
           m, n = n, m if n > m
           m -= n; m >>= 1 while m[0] == 0
         end
         m << b
       end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make it so when a model is created via created action
I have an entry.rb model and I'm trying to make a semi-complicated validation. I
Let's say i have a Domain Model that im trying to make compatible with
I'm trying to solve the relational model in order to make a Django app.
im trying to make Categories and Subcategories, im checking this models but i have
I am using acts_as_list on my Product model, and trying to make it so
This one's really getting me down! :( I'm trying to make a nested model
I am trying to make a custom JSF converter for my category model. The
I'm trying to make Core Data objects inherit from my own custom class rather
I'm trying to make a stripped-down user sessions system, and tried to model it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.