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

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

Only Do What Only You Can Do

順列

順列 (異なる 個のものから 個取ってできる順列の総数)は, 次の式で表せます.

つまり, 下降階乗冪です.

この節では, 下降階乗冪を使って, 異なる $ 10 $ 個のものから $ 5 $ 個取ってできる順列の総数を求めます.

VBScript

'階乗
Private Function Fact(n)
    If n <= 1 Then
        Fact = 1
    Else
        Fact = n * Fact(n - 1)
    End If
End Function

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

'順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
Dim n: n = 10
Dim r: r = 5
WScript.Echo(Fact(n) / Fact(n - r))
WScript.Echo(FallingFact(n, r))
Z:\>cscript //nologo 0406.vbs
30240
30240

JScript

// 階乗
function Fact(n) {
    if (n <= 1)
        return 1
    else
        return n * Fact(n - 1)
}

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

// 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
var n = 10
var r = 5
WScript.Echo(Fact(n) / Fact(n - r))
WScript.Echo(FallingFact(n, r))
Z:\>cscript //nologo 0406.js
30240
30240

PowerShell

# 階乗
function Fact($n)
{
    if ($n -le 1)
    {
        1
    }
    else
    {
        $n * (Fact ($n - 1))
    }
}

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

# 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
$n = 10
$r = 5
Write-Host ((Fact $n) / (Fact ($n - $r)))
Write-Host (FallingFact $n $r)
Z:\>powershell -file 0406.ps1
30240
30240

Perl

# 階乗
sub Fact
{
    my ($n) = @_;

    if ($n <= 1)
    {
        1;
    }
    else
    {
        $n * Fact($n - 1);
    }
}

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

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

# 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
$n = 10;
$r = 5;
print Fact($n) / Fact($n - $r), "\n";
print FallingFact($n, $r), "\n";
Z:\>perl 0406.pl
30240
30240

PHP

<?php
# 階乗
function Fact($n)
{
    if ($n <= 1)
        return 1;
    else
        return $n * Fact($n - 1);
}

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

# 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
$n = 10;
$r = 5;
echo Fact($n) / Fact($n - $r), "\n";
echo FallingFact($n, $r), "\n";
?>
Z:\>php 0406.php
30240
30240

Python

# coding: Shift_JIS

# 階乗
def Fact(n):
    if (n <= 1):
        return 1
    else:
        return n * Fact(n - 1)

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

# 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
n = 10
r = 5
print Fact(n) / Fact(n - r)
print FallingFact(n, r)
Z:\>python 0406.py
30240
30240

Ruby

# 階乗
def Fact(n)
    if (n <= 1)
        1
    else
        n * Fact(n - 1)
    end
end

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

# 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
n = 10
r = 5
puts Fact(n) / Fact(n - r)
puts FallingFact(n, r)
Z:\>ruby 0406.rb
30240
30240

Groovy

Pascal

Program Pas0406(arg);
uses
    SysUtils;

    // 階乗
    function Fact(n: Integer): Longint;
    begin
        if n <= 1 then
            Fact := 1
        else
            Fact := n * Fact(n - 1);
    end;

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

var
    n: Integer;
    r: Integer;
begin
    // 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
    n := 10;
    r := 5;
    writeln(format('%g', [Fact(n) / Fact(n - r)]));
    writeln(FallingFact(n, r));
end.
Z:\>fpc Pas0406.pp -v0
Free Pascal Compiler version 2.6.2 [2013/02/12] for i386
Copyright (c) 1993-2012 by Florian Klaempfl and others

Z:\>Pas0406
30240
30240

Ada

VB.NET

Module VB0406
    '階乗
    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

    '下降階乗冪
    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 個のものから 5 個取ってできる順列の総数)
        Dim n As Integer = 10
        Dim r As Integer = 5
        Console.WriteLine(Fact(n) / Fact(n - r))
        Console.WriteLine(FallingFact(n, r))
    End Sub
End Module
Z:\>vbc -nologo VB0406.vb

Z:\>VB0406
30240
30240

C#

using System;

public class CS0406
{
    // 階乗
    private static int Fact(int n)
    {
        if (n <= 1)
            return 1;
        else
            return n * Fact(n - 1);
    }

    // 下降階乗冪
    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 個のものから 5 個取ってできる順列の総数)
        int n = 10;
        int r = 5;
        Console.WriteLine(Fact(n) / Fact(n - r));
        Console.WriteLine(FallingFact(n, r));
    }
}
Z:\>csc -nologo CS0406.cs

Z:\>CS0406
30240
30240

Java

public class Java0406 {

    // 階乗
    private static int Fact(int n) {
        if (n <= 1)
            return 1;
        else
            return n * Fact(n - 1);
    }

    // 下降階乗冪
    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 個のものから 5 個取ってできる順列の総数)
        int n = 10;
        int r = 5;
        System.out.println(Fact(n) / Fact(n - r));
        System.out.println(FallingFact(n, r));
    }
}
Z:\>javac Java0406.java

Z:\>java Java0406
30240
30240

C++

#include <iostream>

using namespace std;

// 階乗
int Fact(int n)
{
    if (n <= 1)
        return 1;
    else
        return n * Fact(n - 1);
}

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

int main()
{
    // 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
    int n = 10;
    int r = 5;
    cout << (Fact(n) / Fact(n - r)) << endl; 
    cout << FallingFact(n, r) << endl; 
    return 0;
}
Z:\>bcc32 CP0406.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
CP0406.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Z:\>CP0406
30240
30240

Objective-C

#import <Foundation/Foundation.h>

// 階乗
int Fact(int n)
{
    if (n <= 1)
        return 1;
    else
        return n * Fact(n - 1);
}

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

int main()
{
    // 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
    int n = 10;
    int r = 5;
    printf("%d\n", (Fact(n) / Fact(n - r)));
    printf("%d\n", FallingFact(n, r));
    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 Fact(n: Int): Int = {
    n match {
        case 0 => 1
        case _ => n * Fact(n - 1)
    }
}
// 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
val n = 10
val r = 5
Fact(n) / Fact(n - r)
res0: Int = 30240
// 下降階乗冪
def FallingFact(x: Int, n: Int): Int = {
    n match {
        case 1 => x
        case _ => x * FallingFact(x - 1, n - 1)
    }
}
// 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
val n = 10
val r = 5
FallingFact(n, r)
res1: 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 Fact = function
    |   0 -> 1
    |   n -> n * Fact(n - 1)
// 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
let n = 10
let r = 5
Fact n / Fact (n - r)
val it : int = 30240
// 下降階乗冪
let rec FallingFact (x:int) (n:int):int =
    match n with
        | 1 -> x
        | _ -> x * (FallingFact (x - 1) (n - 1))
// 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
let n = 10
let r = 5
FallingFact n r
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 Fact [n]
    (if (zero? n)
        1
        (* n (Fact (- n 1)))))
; 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
(def n 10)
(def r 5)
(quot (Fact n) (Fact (- n r)))
30240
; 下降階乗冪
(defn FallingFact [x n]
    (if (<= n 1)
        x
        (* x (FallingFact (- x 1) (- n 1)))))
; 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
(def n 10)
(def r 5)
(FallingFact n r)
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.
-- 階乗
fact 0 = 1
fact n = n * fact (n-1)
-- 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
let n = 10
let r = 5
(fact n) `div` (fact (n - r))
30240
-- 下降階乗冪
fallingFact x 1 = x
fallingFact x n = x * (fallingFact (x - 1) (n - 1))
-- 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
let n = 10
let r = 5
fallingFact n r
30240

終了

Prelude> :quit
Leaving GHCi.
inserted by FC2 system