さまざまな言語で数値計算
Only Do What Only You Can Do
階乗
階乗 は, 「初項 $ 1 $, 公比 $ 1 $, 項数 $ n $ の等比数列の総乗」なので, 次の式で表せます.
また, 漸化式で次のように表すこともできます.
この節では, 再帰処理を使って, $ 10 $ の階乗を求めます.
VBScript
'階乗を求める関数 Private Function Fact(n) If n <= 1 Then Fact = 1 Else Fact = n * Fact(n - 1) End If End Function '10の階乗 WScript.Echo(Fact(10)) WScript.Echo(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)
Z:\>cscript //nologo 0403.vbs 3628800 3628800
JScript
// 階乗を求める関数 function Fact(n) { if (n <= 1) return 1 else return n * Fact(n - 1) } // 10の階乗 WScript.Echo(Fact(10)) WScript.Echo(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)
Z:\>cscript //nologo 0403.js 3628800 3628800
PowerShell
# 階乗を求める関数 function Fact($n) { if ($n -le 1) { 1 } else { $n * (Fact ($n - 1)) } } # 10の階乗 Write-Host (Fact(10)) Write-Host (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)
Z:\>powershell -file 0403.ps1 3628800 3628800
Perl
# 階乗を求める関数 sub Fact { my ($n) = @_; if ($n <= 1) { 1; } else { $n * Fact($n - 1); } } # 10の階乗 print Fact(10), "\n"; print 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1, "\n";
Z:\>perl 0403.pl 3628800 3628800
PHP
<?php # 階乗を求める関数 function Fact($n) { if ($n <= 1) return 1; else return $n * Fact($n - 1); } # 10の階乗 echo Fact(10), "\n"; echo 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1, "\n"; ?>
Z:\>php 0403.php 3628800 3628800
Python
# coding: Shift_JIS # 階乗を求める関数 def Fact(n): if (n <= 1): return 1 else: return n * Fact(n - 1) # 10の階乗 print Fact(10) print 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
Z:\>python 0403.py 3628800 3628800
Ruby
# 階乗を求める関数 def Fact(n) if (n <= 1) 1 else n * Fact(n - 1) end end # 10の階乗 puts Fact(10) puts 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
Z:\>ruby 0403.rb 3628800 3628800
Groovy
Pascal
Program Pas0403(arg); // 階乗を求める関数 function Fact(n: Integer): Longint; begin if n <= 1 then Fact := 1 else Fact := n * Fact(n - 1); end; begin // 10の階乗 writeln(Fact(10)); writeln(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1); end.
Z:\>fpc Pas0403.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0403 3628800 3628800
Ada
VB.NET
Module VB0403 '階乗を求める関数 Private Function Fact(ByVal n As Integer) As Integer If n <= 1 Then Return 1 Else Return n * Fact(n - 1) End If End Function Sub Main() '10の階乗 Console.WriteLine(Fact(10)) Console.WriteLine(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) End Sub End Module
Z:\>vbc -nologo VB0403.vb Z:\>VB0403 3628800 3628800
C#
using System; public class CS0403 { // 階乗を求める関数 private static int Fact(int n) { if (n <= 1) return 1; else return n * Fact(n - 1); } public static void Main() { // 10の階乗 Console.WriteLine(Fact(10)); Console.WriteLine(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1); } }
Z:\>csc -nologo CS0403.cs Z:\>CS0403 3628800 3628800
Java
public class Java0403 { // 階乗を求める関数 private static int Fact(int n) { if (n <= 1) return 1; else return n * Fact(n - 1); } public static void main(String []args) { // 10の階乗 System.out.println(Fact(10)); System.out.println(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1); } }
Z:\>javac Java0403.java Z:\>java Java0403 3628800 3628800
C++
#include <iostream> using namespace std; // 階乗を求める関数 int Fact(int n) { if (n <= 1) return 1; else return n * Fact(n - 1); } int main() { // 10の階乗 cout << Fact(10) << endl; cout << (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) << endl; return 0; }
Z:\>bcc32 CP0403.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0403.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0403 3628800 3628800
Objective-C
#import <Foundation/Foundation.h> // 階乗を求める関数 int Fact(int n) { if (n <= 1) return 1; else return n * Fact(n - 1); } int main() { // 10の階乗 printf("%d\n", Fact(10)); printf("%d\n", 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1); return 0; }
Compiling the source code.... $gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base main.m -o demo -lm -pthread -lgmpxx -lreadline 2>&1 Executing the program.... $demo 3628800 3628800
D
Go
Scala
対話型実行環境を起動
Z:\>scala Welcome to Scala version 2.10.2 (Java HotSpot(TM) Client VM, Java 1.7.0_21). Type in expressions to have them evaluated. Type :help for more information.
// 階乗を求める関数 def Fact(n: Int): Int = { n match { case 0 => 1 case _ => n * Fact(n - 1) } }
// 10の階乗
Fact(10)
res0: Integer = 3628800
終了
scala> :quit
F#
対話型実行環境を起動
Z:\>fsi Microsoft (R) F# 2.0 Interactive build 4.0.40219.1 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;;
// 階乗を求める関数 let rec Fact = function | 0 -> 1 | n -> n * Fact(n - 1)
// 10の階乗
Fact 10
val it : int = 3628800
終了
> #quit;;
Clojure
対話型実行環境を起動
Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main Clojure 1.5.1
; 階乗を求める関数 (defn Fact [n] (if (zero? n) 1 (* n (Fact (- n 1)))))
; 10の階乗
(Fact 10)
3628800
終了
user=> (. System exit 0)
Haskell
対話型実行環境を起動
Z:\>ghci GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done.
-- 階乗を求める関数
fact 0 = 1
fact n = n * fact (n-1)
-- 10の階乗
fact 10
3628800
終了
Prelude> :quit Leaving GHCi.