Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

GCF and LCD?

I need functions for Greatest Common Factor and Least Common Denominator. I was able to create a GCF for myself, but I can't figure out LCD, and frankly I'm astonished it's not in the base code. I've searched everywhere in the Help File's code structure...

Until somebody points out a Ruby LCD function I'll just keep trying to figure it out on my own.

EDIT: Ugh. Wasn't that hard. I think I've got a working LCD function now...although I'm still majorly dissapointed these functions didn't get created in the Enumerable module.

And by the way, I know for me it would be a good feature to be able to delete my own threads if no one's posted in them yet...that way my random RGSS quirks don't have to be filtered through by moderators and closed.
 
Theres a lot of things they didn't just install directly into the ruby library.

But like you said, if you need to know something like getting the LCD, you should be able to do it (or you wouldn't really know what a LCD is ;)) and more than likey, you could add it to the library yourself. Just my thoughts. The rule goes for about anything.

And the privs to delete your own topics could be abused severelly. Morons could flood the forum just because.
 
Oh well. I'm getting problems with my LCD method, it's returning the LCD of 4 and 8 as 16. Weird.

EDIT: Found my problem, and fixed it. Heh. Now my problem is, the program's doing so much, I get a script hanging error. LOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOL
 
I think I may have created a LCD method in my Monster Arena script I'll try to dig it up

call me the class and method packrat...

Greatest common denominator/factor uses Euclids method

Code:
  def gcd(numer,denom)
    a = numer
    b = denom
    while b > 0
      r = a % b
      a = b
      b = r
    end
    return a
  end
 
I probably did 1000x too much, but I did something that will get factors of any numbers, and get the GCF of any list of numbers (an array). Do not laugh. It's been years since I have looked at GCF, so I am just glad to remember what it is. :P

Code:
class Integer < Numeric
  def factors
    dummy = self
    factors = []
    loop do
      break if dummy == 1
      for i in 2..dummy
        if dummy % i == 0
          dummy /= i
          factors << i
          redo
        end
      end
      break
    end
    return factors.sort
  end
end

class Array
  def delete_once(n)
    for i in 0...self.size
      if self[i] == n
        self.delete_at(i)
        break
      end
    end
  end
  def sum
    n = 0
    self.each {|x| n += x}
    return n
  end
  def greatest_common_factor
    return nil if self.empty?
    if self.size == 1
      return self[0].factors.sum
    end
    factor_trees = []
    self.each {|x| factor_trees << x.factors}
    gcfs = []
    for n in factor_trees[0]
      add = true
      for i in 1...factor_trees.size
        unless factor_trees[i].include?(n)
          add = false
        end
      end
      if add
        gcfs << n
        for i in 1...factor_trees.size
          factor_trees[i].delete_once(n)
        end
      end
    end
    gcf = 1
    gcfs.each {|x| gcf *= x}
    return gcf
  end
end

I think that works as I have tested it a few different ways.

Anyways, to use it : <array>.greatest_common_factor

There is probably a much easier way, but I did this up real quick, so leave me alone Mr. Calculus ;)
 
....
You did one necessary thing :S

Code:
      for i in 2..dummy

There's never going to be a factor greater than the Square root of the number sent to it, there's probably some super simple efficient way of doing it but I don't know of it.
 
My method tests every single number between 1 and the lowest number in the series, for GCF, then figures out LCD with the GCF. I'll post it up later, right now it's too much trouble to access the RX project.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top