home > さまざまな言語で数値計算 > 数列の積・階乗・順列・組合せ >

さまざまな言語で数値計算

Only Do What Only You Can Do

下降階乗冪

下降階乗冪は, から負の向きに 個の数の総乗であり, 次の式で表せます.

また, 漸化式で次のように表すこともできます.

この節では, 再帰処理を使って, $ 10 $ から負の向きに $ 5 $ 個の数の総乗を求めます.

VBScript

'下降階乗冪
Private Function FallingFact(x, n)
    If n <= 1 Then
        FallingFact = x
    Else
        FallingFact = x * FallingFact(x - 1, n - 1)
    End If
End Function

'10 から 6 までの 総乗
WScript.Echo(FallingFact(10, 5))
WScript.Echo(10 * 9 * 8 * 7 * 6)
Z:\>cscript //nologo 0404.vbs
30240
30240

JScript

// 下降階乗冪
function FallingFact(x, n) {
    if (n <= 1)
        return x
    else
        return x * FallingFact(x - 1, n - 1)
}

// 10 から 6 までの 総乗
WScript.Echo(FallingFact(10, 5))
WScript.Echo(10 * 9 * 8 * 7 * 6)
Z:\>cscript //nologo 0404.js
30240
30240

PowerShell

# 下降階乗冪
function FallingFact($x, $n)
{
    if ($n -le 1)
    {
        $x
    }
    else
    {
        $x * (FallingFact ($x - 1) ($n - 1))
    }
}

# 10 から 6 までの 総乗
Write-Host (FallingFact 10 5)
Write-Host (10 * 9 * 8 * 7 * 6)
Z:\>powershell -file 0404.ps1
30240
30240

Perl

# 下降階乗冪
sub FallingFact
{
    my ($x, $n) = @_;

    if ($n <= 1)
    {
        $x;
    }
    else
    {
        $x * FallingFact($x - 1, $n - 1);
    }
}

# 10 から 6 までの 総乗
print FallingFact(10, 5), "\n";
print 10 * 9 * 8 * 7 * 6, "\n";
Z:\>perl 0404.pl
30240
30240

PHP

<?php
# 下降階乗冪
function FallingFact($x, $n)
{
    if ($n <= 1)
        return $x;
    else
        return $x * FallingFact($x - 1, $n - 1);
}

# 10 から 6 までの 総乗
echo FallingFact(10, 5), "\n";
echo 10 * 9 * 8 * 7 * 6, "\n";
?>
Z:\>php 0404.php
30240
30240

Python

# 下降階乗冪
def FallingFact(x, n):
    if (n <= 1):
        return x
    else:
        return x * FallingFact(x - 1, n - 1)

# 10 から 6 までの 総乗
print FallingFact(10, 5)
print 10 * 9 * 8 * 7 * 6
Z:\>python 0404.py
30240
30240

Ruby

# 下降階乗冪
def FallingFact(x, n)
    if (n <= 1)
        x
    else
        x * FallingFact(x - 1, n - 1)
    end
end

# 10 から 6 までの 総乗
puts FallingFact(10, 5)
puts 10 * 9 * 8 * 7 * 6
Z:\>ruby 0404.rb
30240
30240

Groovy

Pascal

Program Pas0404(arg);

    // 下降階乗冪
    function FallingFact(x: Integer; n: Integer): Longint;
    begin
        if n <= 1 then
            FallingFact := x
        else
            FallingFact := x * FallingFact(x - 1, n - 1);
    end;

begin
    // 10 から 6 までの 総乗
    writeln(FallingFact(10, 5));
    writeln(10 * 9 * 8 * 7 * 6);
end.
Z:\>fpc Pas0404.pp -v0
Free Pascal Compiler version 2.6.2 [2013/02/12] for i386
Copyright (c) 1993-2012 by Florian Klaempfl and others

Z:\>Pas0404
30240
30240

Ada

VB.NET

Module VB0404
    '下降階乗冪
    Private Function FallingFact(ByVal x As Integer, ByVal n As Integer) As Integer
        If n <= 1 Then
            Return x
        Else
            Return x * FallingFact(x - 1, n - 1)
        End If
    End Function

    Sub Main()
        '10 から 6 までの 総乗
        Console.WriteLine(FallingFact(10, 5))
        Console.WriteLine(10 * 9 * 8 * 7 * 6)
    End Sub
End Module
Z:\>vbc -nologo VB0404.vb

Z:\>VB0404
30240
30240

C#

using System;

public class CS0404
{
    // 下降階乗冪
    private static int FallingFact(int x, int n)
    {
        if (n <= 1)
            return x;
        else
            return x * FallingFact(x - 1, n - 1);
    }

    public static void Main()
    {
        // 10 から 6 までの 総乗
        Console.WriteLine(FallingFact(10, 5));
        Console.WriteLine(10 * 9 * 8 * 7 * 6);
    }
}
Z:\>csc -nologo CS0404.cs

Z:\>CS0404
30240
30240

Java

public class Java0404 {
    // 下降階乗冪
    private static int FallingFact(int x, int n) {
        if (n <= 1)
            return x;
        else
            return x * FallingFact(x - 1, n - 1);
    }

     public static void main(String []args) {
        // 10 から 6 までの 総乗
        System.out.println(FallingFact(10, 5));
        System.out.println(10 * 9 * 8 * 7 * 6);
    }
}
Z:\>javac Java0404.java

Z:\>java Java0404
30240
30240

C++

#include <iostream>

using namespace std;

// 下降階乗冪
int FallingFact(int x, int n)
{
    if (n <= 1)
        return x;
    else
        return x * FallingFact(x - 1, n - 1);
}

int main()
{
    // 10 から 6 までの 総乗
    cout << FallingFact(10, 5) << endl; 
    cout << (10 * 9 * 8 * 7 * 6) << endl; 
    return 0;
}
Z:\>bcc32 CP0404.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
CP0404.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Z:\>CP0404
30240
30240

Objective-C

#import <Foundation/Foundation.h>

// 下降階乗冪
int FallingFact(int x, int n)
{
    if (n <= 1)
        return x;
    else
        return x * FallingFact(x - 1, n - 1);
}

int main()
{
    // 10 から 6 までの 総乗
    printf("%d\n", FallingFact(10, 5));
    printf("%d\n", 10 * 9 * 8 * 7 * 6);
    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
30240
30240

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 FallingFact(x: Int, n: Int): Int = {
    n match {
        case 1 => x
        case _ => x * FallingFact(x - 1, n - 1)
    }
}
// 10 から 6 までの 総乗
FallingFact(10, 5)
res0: Integer = 30240

終了

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 FallingFact (x:int) (n:int):int =
    match n with
        | 1 -> x
        | _ -> x * (FallingFact (x - 1) (n - 1))
// 10 から 6 までの 総乗
FallingFact 10 5
val it : int = 30240

終了

> #quit;;

Clojure

対話型実行環境を起動

Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main
Clojure 1.5.1
; 下降階乗冪
(defn FallingFact [x n]
    (if (<= n 1)
        x
        (* x (FallingFact (- x 1) (- n 1)))))
; 10 から 6 までの 総乗
(FallingFact 10 5)
30240

終了

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.
-- 下降階乗冪
fallingFact x 1 = x
fallingFact x n = x * (fallingFact (x - 1) (n - 1))
-- 10 から 6 までの 総乗
fallingFact 10 5
30240

終了

Prelude> :quit
Leaving GHCi.
inserted by FC2 system