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

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

Only Do What Only You Can Do

上昇階乗冪

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

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

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

VBScript

'上昇階乗冪
Private Function RisingFact(x, n)
    If n <= 1 Then
        RisingFact = x
    Else
        RisingFact = x * RisingFact(x + 1, n - 1)
    End If
End Function

'10 から 14 までの 総乗
WScript.Echo(RisingFact(10, 5))
WScript.Echo(10 * 11 * 12 * 13 * 14)
Z:\>cscript //nologo 0405.vbs
240240
240240

JScript

// 上昇階乗冪
function RisingFact(x, n) {
    if (n <= 1)
        return x
    else
        return x * RisingFact(x + 1, n - 1)
}

// 10 から 14 までの 総乗
WScript.Echo(RisingFact(10, 5))
WScript.Echo(10 * 11 * 12 * 13 * 14)
Z:\>cscript //nologo 0405.js
240240
240240

PowerShell

# 上昇階乗冪
function RisingFact($x, $n)
{
    if ($n -le 1)
    {
        $x
    }
    else
    {
        $x * (RisingFact ($x + 1) ($n - 1))
    }
}

# 10 から 14 までの 総乗
Write-Host (RisingFact 10 5)
Write-Host (10 * 11 * 12 * 13 * 14)
Z:\>powershell -file 0405.ps1
240240
240240

Perl

# 上昇階乗冪
sub RisingFact
{
    my ($x, $n) = @_;

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

# 10 から 14 までの 総乗
print RisingFact(10, 5), "\n";
print 10 * 11 * 12 * 13 * 14, "\n";
Z:\>perl 0405.pl
240240
240240

PHP

# 上昇階乗冪
function RisingFact($x, $n)
{
    if ($n <= 1)
        return $x;
    else
        return $x * RisingFact($x + 1, $n - 1);
}

# 10 から 14 までの 総乗
echo RisingFact(10, 5), "\n";
echo 10 * 11 * 12 * 13 * 14, "\n";
Z:\>php 0405.php
240240
240240

Python

# 上昇階乗冪
def RisingFact(x, n):
    if (n <= 1):
        return x
    else:
        return x * RisingFact(x + 1, n - 1)

# 10 から 14 までの 総乗
print RisingFact(10, 5)
print 10 * 11 * 12 * 13 * 14
Z:\>python 0405.py
240240
240240

Ruby

# 上昇階乗冪
def RisingFact(x, n)
    if (n <= 1)
        x
    else
        x * RisingFact(x + 1, n - 1)
    end
end

# 10 から 14 までの 総乗
puts RisingFact(10, 5)
puts 10 * 11 * 12 * 13 * 14
Z:\>ruby 0405.rb
240240
240240

Groovy

Pascal

Program Pas0405(arg);

    // 上昇階乗冪
    function RisingFact(x: Integer; n: Integer): Longint;
    begin
        if n <= 1 then
            RisingFact := x
        else
            RisingFact := x * RisingFact(x + 1, n - 1);
    end;

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

Z:\>Pas0405
240240
240240

Ada

VB.NET

Module VB0405
    '上昇階乗冪
    Private Function RisingFact(ByVal x As Integer, ByVal n As Integer) As Integer
        If n <= 1 Then
            Return x
        Else
            Return x * RisingFact(x + 1, n - 1)
        End If
    End Function

    Sub Main()
        '10 から 14 までの 総乗
        Console.WriteLine(RisingFact(10, 5))
        Console.WriteLine(10 * 11 * 12 * 13 * 14)
    End Sub
End Module
Z:\>vbc -nologo VB0405.vb

Z:\>VB0405
240240
240240

C#

using System;

public class CS0405
{
    // 上昇階乗冪
    private static int RisingFact(int x, int n)
    {
        if (n <= 1)
            return x;
        else
            return x * RisingFact(x + 1, n - 1);
    }

    public static void Main()
    {
        // 10 から 14 までの 総乗
        Console.WriteLine(RisingFact(10, 5));
        Console.WriteLine(10 * 11 * 12 * 13 * 14);
    }
}
Z:\>csc -nologo CS0405.cs

Z:\>CS0405
240240
240240

Java

public class Java0405 {
    // 上昇階乗冪
    private static int RisingFact(int x, int n) {
        if (n <= 1)
            return x;
        else
            return x * RisingFact(x + 1, n - 1);
    }

     public static void main(String []args) {
        // 10 から 14 までの 総乗
        System.out.println(RisingFact(10, 5));
        System.out.println(10 * 11 * 12 * 13 * 14);
    }
}
Z:\>javac Java0405.java

Z:\>java Java0405
240240
240240

C++

#include <iostream>

using namespace std;

// 上昇階乗冪
int RisingFact(int x, int n)
{
    if (n <= 1)
        return x;
    else
        return x * RisingFact(x + 1, n - 1);
}

int main()
{
    // 10 から 14 までの 総乗
    cout << RisingFact(10, 5) << endl; 
    cout << (10 * 11 * 12 * 13 * 14) << endl; 
    return 0;
}
Z:\>bcc32 CP0405.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
CP0405.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Z:\>CP0405
240240
240240

Objective-C

#import <Foundation/Foundation.h>

// 上昇階乗冪
int RisingFact(int x, int n)
{
    if (n <= 1)
        return x;
    else
        return x * RisingFact(x + 1, n - 1);
}

int main()
{
    // 10 から 14 までの 総乗
    printf("%d\n", RisingFact(10, 5));
    printf("%d\n", 10 * 11 * 12 * 13 * 14);
    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....
240240
240240

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

終了

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

終了

> #quit;;

Clojure

対話型実行環境を起動

Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main
Clojure 1.5.1
; 上昇階乗冪
(defn RisingFact [x n]
    (if (<= n 1)
        x
        (* x (RisingFact (+ x 1) (- n 1)))))
; 10 から 14 までの 総乗
(RisingFact 10 5)
240240

終了

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.
-- 上昇階乗冪
risingFact x 1 = x
risingFact x n = x * (risingFact (x + 1) (n - 1))
-- 10 から 14 までの 総乗
risingFact 10 5
240240

終了

Prelude> :quit
Leaving GHCi.
inserted by FC2 system