さまざまな言語で数値計算
Only Do What Only You Can Do
数列の積 (再帰版)
初項 $ a $, 公差 $ d $ の等差数列の初項から第 $ n $ 項までの積は, 次の式で表せます.
また, 漸化式で次のように表すこともできます.
(TODO: 漸化式の図を挿入)
再帰を使うと, 漸化式をそのままコーディングできます.
この節では, 再帰処理を使って, 初項 $ 5 $, 公差 $ 3 $, 項数 $ 10 $ の数列の積を求めます.
VBScript
Option Explicit '初項 5, 公差 3, 項数 10 の数列の積を表示する WScript.Echo product(5, 3, 10) Private Function product(m, d, n) If n = 0 Then product = 1 Else product = m * product(m + d, d, n - 1) End If End Function
Z:\>cscript //nologo 0402.vbs 1162274713600
JScript
// 初項 5, 公差 3, 項数 10 の数列の積を表示する WScript.Echo(product(5, 3, 10)) function product(m, d, n) { if (n == 0) return 1 else return m * product(m + d, d, n - 1) }
Z:\>cscript //nologo 0402.js 1162274713600
PowerShell
# 初項 5, 公差 3, 項数 10 の数列の積を表示する Write-Host (product 5 3 10) function product($m, $d, $n) { if ($n -eq 0) { 1 } else { $m * (product ($m + $d) $d ($n - 1)) } }
Z:\>powershell -file 0402.ps1 1162274713600
Perl
sub product { my ($m, $d, $n) = @_; if ($n == 0) { 1 } else { $m * product($m + $d, $d, $n - 1) } } # 初項 5, 公差 3, 項数 10 の数列の積を表示する print product(5, 3, 10);
Z:\>perl 0402.pl 1162274713600
PHP
<?php # 初項 5, 公差 3, 項数 10 の数列の積を表示する echo product(5, 3, 10); function product($m, $d, $n) { if ($n == 0) return 1; else return $m * product($m + $d, $d, $n - 1); } ?>
Z:\>php 0402.php 1162274713600
Python
def product(m, d, n): if (n == 0): return 1 else: return m * product(m + d, d, n - 1) # 初項 5, 公差 3, 項数 10 の数列の積を表示する print product(5, 3, 10)
Z:\>python 0402.py 1162274713600
Ruby
def product(m, d, n) if (n == 0) 1 else m * product(m + d, d, n - 1) end end # 初項 5, 公差 3, 項数 10 の数列の積を表示する puts product(5, 3, 10)
Z:\>ruby 0402.rb 1162274713600
Groovy
Pascal
Program Pas0402(arg); {$MODE delphi} function product(m:Integer; d:Integer; n:Integer):Int64; begin if n = 0 then result := 1 else result := m * product(m + d, d, n - 1); end; begin // 初項 5, 公差 3, 項数 10 の数列の積を表示する writeln(product(5, 3, 10)); end.
Z:\>fpc Pas0402.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0402 1162274713600
Ada
VB.NET
Option Explicit Module VB0402 Sub Main() '初項 5, 公差 3, 項数 10 の数列の積を表示する Console.WriteLine(product(5, 3, 10)) End Sub Private Function product(ByVal m As Integer, ByVal d As Integer, ByVal n As Integer) As Long If n = 0 Then Return 1 Else Return m * product(m + d, d, n - 1) End If End Function End Module
Z:\>vbc -nologo VB0402.vb Z:\>VB0402 1162274713600
C#
using System; public class CS0402 { public static void Main() { // 初項 5, 公差 3, 項数 10 の数列の積を表示する Console.WriteLine(product(5, 3, 10)); } private static long product(int m, int d, int n) { if (n == 0) return 1; else return m * product(m + d, d, n - 1); } }
Z:\>csc -nologo CS0402.cs Z:\>CS0402 1162274713600
Java
public class Java0402 { public static void main(String []args) { // 初項 5, 公差 3, 項数 10 の数列の積を表示する System.out.println(product(5, 3, 10)); } private static long product(int m, int d, int n) { if (n == 0) return 1; else return m * product(m + d, d, n - 1); } }
Z:\>javac Java0402.java Z:\>java Java0402 1162274713600
C++
#include <iostream> using namespace std; __int64 product(int m, int d, int n); int main() { // 初項 5, 公差 3, 項数 10 の数列の積を表示する cout << product(5, 3, 10) << endl; return 0; } __int64 product(int m, int d, int n) { if (n == 0) return 1; else return m * product(m + d, d, n - 1); }
Z:\>bcc32 CP0402.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0402.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0402 1162274713600
Objective-C
#import <Foundation/Foundation.h> long product(int m, int d, int n); int main (int argc, const char * argv[]) { // 初項 5, 公差 3, 項数 10 の数列の積を表示する printf("%ld\n", product(5, 3, 10)); return 0; } long product(int m, int d, int n) { if (n == 0) return 1; else return m * product(m + d, d, n - 1); }
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 1162274713600
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 product(m: Long, d: Int, n: Int): Long = { n match { case 0 => 1 case _ => m * product(m + d, d, n - 1) } }
// 初項 5, 公差 3, 項数 10 の数列の積
product(5, 3, 10)
res0: Long = 1162274713600
終了
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 product (m:int64) (d:int64) (n:int64) :int64 = match n with | 0L -> 1L | _ -> m * (product (m + d) d (n - 1L))
// 初項 5, 公差 3, 項数 10 の数列の積
product 5L 3L 10L
val it : int64 = 1162274713600L
終了
> #quit;;
Clojure
対話型実行環境を起動
Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main Clojure 1.5.1
; 等差数列の積 (defn product [m d n] (if (zero? n) 1 (* m (product (+ m d) d (- n 1)))))
; 初項 5, 公差 3, 項数 10 の数列の積
(product 5 3 10)
1162274713600
終了
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.
-- 等差数列の積
prod m d 0 = 1
prod m d n = m * (prod (m + d) d (n - 1))
対話型実行環境で複数行を渡す場合は, 以下のように ":{~:}" で括る.
さらに, 関数定義は "let {~}" で括る.
Prelude> :{ Prelude| let { Prelude| prod m d 0 = 1; Prelude| prod m d n = m * (prod (m + d) d (n - 1)); Prelude| } Prelude| :}
-- 初項 5, 公差 3, 項数 10 の数列の積
prod 5 3 10
1162274713600
終了
Prelude> :quit Leaving GHCi.