0
0

素因数分解をするrubyスクリプト

Last updated at Posted at 2023-11-25

素因数分解をするrubyスクリプトです。
chmod +x factor.rb
./factor.rb <n>
として動かしてください。

factor.rb
#!/usr/bin/ruby
def primefactorization(n)
  if n==0 || n==1
    return []
  end
  i=2
  f=[]
  while n!=1 do
    if n%i==0
      f+=[i]
      n/=i
    else
      i=i+1
    end
  end
  f
end

n=ARGV[0].to_i
f=primefactorization(n)
print n,":",f,"\n"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0