さまざまな言語で数値計算
Only Do What Only You Can Do
自然数の3乗の和
自然数の3乗の和は, 次の式で表せます.
この公式を使って, $ 100 $ までの自然数の3乗の和を求めます.
VBScript
Option Explicit ' 100 までの 自然数の3乗の和 Dim n: n = 100 WScript.Echo (n ^ 2) * ((n + 1) ^ 2) \ 4
Z:\>cscript //nologo 0306.vbs 25502500
JScript
// 100 までの 自然数の3乗の和 var n = 100 WScript.Echo(Math.pow(n, 2) * (Math.pow((n + 1), 2)) / 4)
Z:\>cscript //nologo 0306.js 25502500
PowerShell
# 100 までの 自然数の3乗の和 $n = 100 Write-Host([math]::pow($n, 2) * [math]::pow(($n + 1), 2) / 4)
Z:\>powershell -file 0306.ps1 25502500
Perl
# 100 までの 自然数の3乗の和 my $n = 100; print(($n ** 2) * (($n + 1) ** 2) / 4, "\n");
Z:\>perl 0306.pl 25502500
PHP
<?php # 100 までの 自然数の3乗の和 $n = 100; echo (pow($n, 2) * pow(($n + 1), 2) / 4), "\n"; ?>
Z:\>php 0306.php 25502500
Python
# 100 までの 自然数の3乗の和 n = 100 print ((n ** 2) * ((n + 1) ** 2) / 4)
Z:\>python 0306.py 25502500
Ruby
# 100 までの 自然数の3乗の和 n = 100 puts ((n ** 2) * ((n + 1) ** 2) / 4)
Z:\>ruby 0306.rb 25502500
Groovy
Pascal
Program Pas0306(arg); uses SysUtils, Math; var n:Integer; begin // 100 までの 自然数の3乗の和 n := 100; writeln( format('%g', [(power(n, 2) * power((n + 1), 2) / 4)]) ); end.
Z:\>fpc Pas0306.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0306 25502500
Ada
VB.NET
Module VB0306 Sub Main() ' 100 までの 自然数の3乗の和 Dim n As Integer = 100 Console.WriteLine( (n ^ 2) * ((n + 1) ^ 2) \ 4 ) End Sub End Module
Z:\>vbc -nologo VB0306.vb Z:\>VB0306 25502500
C#
public class CS0306 { public static void Main() { // 100 までの 自然数の3乗の和 int n = 100; System.Console.WriteLine( System.Math.Pow(n, 2) * System.Math.Pow((n + 1), 2) / 4 ); } }
Z:\>csc -nologo CS0306.cs Z:\>CS0306 25502500
Java
public class Java0306{ public static void main(String []args){ // 100 までの 自然数の3乗の和 int n = 100; System.out.println((int) (Math.pow(n, 2) * Math.pow((n + 1), 2) / 4 ) ); } }
Z:\>javac Java0306.java Z:\>java Java0306 25502500
C++
#include <iostream> #include <math.h> using namespace std; int main() { // 100 までの 自然数の3乗の和 int n = 100; cout << int(pow(n, 2) * pow((n + 1), 2) / 4 ) << endl; return 0; }
Z:\>bcc32 CP0306.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0306.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0306 25502500
Objective-C
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { // 100 までの 自然数の3乗の和 int n = 100; printf("%d\n", (int)(pow(n, 2) * pow((n + 1), 2) / 4) ); 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 25502500
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.
100 までの自然数の3乗の和
val n = 100 (Math.pow(n, 2) * Math.pow((n + 1), 2) / 4).toInt
res0: Int = 25502500
終了
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;;
100 までの自然数の3乗の和
let n = 100 System.Math.Pow(double n, 2.0) * System.Math.Pow((double n + 1.0), 2.0) / 4.0
val it : float = 25502500.0
終了
> #quit;;
Clojure
対話型実行環境を起動
Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main Clojure 1.5.1
100 までの自然数の3乗の和
(def n 100) (int (quot (* (Math/pow n 2) (Math/pow (+ n 1) 2)) 4))
25502500
終了
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.
100 までの自然数の3乗の和
let n = 100 (n ^ 2) * ((n + 1) ^ 2) `div` 4
25502500
終了
Prelude> :quit Leaving GHCi.