File: fastmath.rb

package info (click to toggle)
ruby-inline 3.12.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 408 kB
  • sloc: ruby: 1,504; makefile: 2
file content (27 lines) | stat: -rw-r--r-- 469 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

begin require 'rubygems' rescue LoadError end
require 'inline'

class FastMath
  def factorial(n)
    f = 1
    n.downto(2) { |x| f *= x }
    return f
  end
  inline do |builder|
    builder.c "
    long factorial_c(int max) {
      int i=max, result=1;
      while (i >= 2) { result *= i--; }
      return result;
    }"
  end
end

math = FastMath.new

if ARGV.empty? then
  30000.times do math.factorial(20); end
else
  30000.times do math.factorial_c(20); end
end