Browse | Submit A New Snippet | Create A Package

 

Snippets by category: Math Functions

Snippet ID Title
Packages Of Snippets33Language Numberszhang bee
Some kinds of Numbers are in English!
38Surd arithmeticDick Pountain
Permits arithmetic using irrational roots in symbolic form, like √2 or 3+√5 from a Ruby program.
39Surd arithmeticDick Pountain
Let's you return values including irrational roots like √2 or 3+√5 from a Ruby program, by defining a class Surd.
76laplaceWictor Borisov
Laplace transform inverse
77laplaceWictor Borisov
Laplace transform inverse
78laplaceWictor Borisov
Laplace transform inverse
Snippets
8Commify a numberTom Copeland
Commifies a number. Preserved from Warren Brown's ruby-talk posting here - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/78103.
20[ 2 , 4 ] * [ 5, 5 ]Rodrigo Bermejo
Array multiplication
89Range SumRobert Myers
function that returns the sum of every number within a range, without the use of looping.
90Calculate Air DensityMichael Vera
Caclulate Air Density with C++
92Primes / FactoringJustin Smith
A class which provides a listing of primes, via an "each" method.
Also provides a function to produce the prime factors of a given number.

Feedback is welcome.
104Commify a NumberCraig Kim
A convenient function to commify a number or number string using Tom Copeland's snippet. Examples:

irb(main):009:0> commify(123456)
=> "123,456"
irb(main):010:0> commify('123456')
=> "123,456"
irb(main):011:0> commify('123456','_')
=> "123_456"
irb(main):012:0> commify(123456,'_')
=> "123_456"
107convert time in seconds to uptimeErik Hollensbe
This will take a time in seconds (for instance, from a delta of Time.now.to_i at two different times), and convert that to a number of days, hours, minutes, and seconds.

Note: if you want to add weeks and so on, merely figure out the magic number for that amount and put it at the head of the list, the loop will figure it out.

135Roman Numerals
Completely pointless class that converts integers to Roman numerals and vice versa, also extends the integer class with a "to_roman" method. Using method missing as syntactic sugar for converting numerals to numbers.
150Date/Rational/Fixnum#gcd hack increased performance by 15%Kurt Stephens

Date uses Rational heavily, which calls Integer#gcd for every new Rational. The Integer#gcd method is generic to handle Bignums, but performs terribly for Fixnum#gcd(Fixnum), which is probably the most often case.

My RubyInline hack saved 15% execution time in a large Rails application:
152Quadratic Formulapatrick fitzstephens
A quadratic formula script that gets the inputs of the A, B and C numbers and then finds the roots for those numbers.

If you don't know how the quadratic formula works go to: http://www.purplemath.com/modules/quadform.htm
162Prime numbersMichael Behan
Extends the Fixnum class with a "primes" method. `100.primes` will return all the prime numbers between 1 and 100.
165extendible multidimensional array with compact storageJan Wedekind
This is an early version of a class for multidimensional arrays. New element-types and coercions can be added later. Elementwise operations (such as "+" and "-") are very slow at the moment.
254Number WangPhilip Matarese
An experiment with Ascii Art and Ruby code.
273Quick ParseTomokiyo Nomura
A simpe programmable parser. A Lex class object is created with a rex_rule which is an array of [/regexp as a rule/, a character as a type of token, function to applay the value of token] elements. A Parse class object is created with a parse_rule which is an array of [/regexp as a rule/, type of token after reduction, function to apply when match]. A sample program is attached.
197Jaccard's and Simple Matching CoefficientsDerick Fay
Calculate Jaccard's Coefficient, Simple Matching Coefficients, and Simple Matching Distance for a pair of vectors. Written for clarity.
198math ( SWEDISH )Ale cson
simple sccript to calculate math?
275PEG parser generator classTomokiyo Nomura
PEG parser gennerator class. The grammar described by PEG will be easily implemented using Peg class. An example program is attached.
302Round to closest multiple of...Skye Weir-Mathews
You pass this function a number you want you rounded, and the multiple you want it rounded to and it passes back the rounded number.
343Language Numberszhang bee
Language Numbers
345EnglishNumeralszhang bee
=begin
While we normally write numbers using Arabic (or since Quiz #22,
Roman) numerals, numbers can also be written out as English phrases.

For example:

7 == seven (the hard way)
42 == forty-two (a very important number)
2001 == two thousand and one (a space odyssey)
1999 == (party like it's) nineteen hundred and ninety-nine


So the quiz is a problem from a Pi Mu Epsilon (US national math club)
newsletter:


"When the integers 1 to 10_000_000_000 are written in the English
language, then sorted as strings, which odd number appears first in
the list?"

- Create Ruby code to translate a number to it's English language form.

- Determine programatically which odd number in 1..10_000_000_000
would sort first if written in English. (Brute force is the obvious
solution, but the computer may have to think about it...)

- Would the answer change for a larger range of values, say 10**30?

- Do French and German Rubyists get a different answer than the
Americans?

=end
445MathExtDick Pountain
Provides a Factorize function and enhances the print formats (to_s method) of Ruby's built-in Complex, Floatand Rational types
446SurdsDick Pountain
Adds a new class Surd to Ruby, which permits arithmetic using irrational roots like √2 and √3. Also provides a function to find the roots of a quadratic equation in terms of surds, rather than floats.
481BetweenMahmoud Abu Arafeh
check if num between r1 and r2
522Use raphaeljs to create graph with ruby dataDaihua ye
Use raphaeljs to create graph with ruby data