LoginSignup
0
0

主要言語で任意精度演算

Last updated at Posted at 2023-08-15

JavaScript

fig.html
<!DOCTYPE html>
<html>
<head>
</head>

<body>
<pre id="output"></pre>
<script>

"use strict";

onload = function() {
	let log = "";
	let a = "3";
	let b = "3";
	for (let i = 0; i < 20; i++) {
		const c = BigInt(a) * BigInt(b);
		log += `${i+1}: ${a} * ${b} = ${c}\n`;
		a = "2" + a;
		b = "4" + b;
	}
	output.textContent = log;
};

</script>
</body>
</html>

Python

Python 3ではintに上限はない模様。

fig.py
# Python 3
a = "3"
b = "3"
for i in range(20):
	c = int(a) * int(b)
	print(f"{i+1}: {a} * {b} = {c}")
	a = "2" + a
	b = "4" + b

Decimalの例
fig.py
# Python 3
from decimal import Decimal, getcontext
getcontext().prec = 40
a = "3"
b = "3"
for i in range(20):
	c = Decimal(a) * Decimal(b)
	print(f"{i+1}: {a} * {b} = {c}")
	a = "2" + a
	b = "4" + b

https://tio.run/##RY1BDsIgEEXXnVNMcAOtMbXtqok7D@AVAGklsUAICw3h7Ehro6v/X/JmvnuHhzV9zge8bRV7mLxd8K6kXvgT9eKsD3j94hFnFaQ1Qb0C/CtlJ@eVxAsOLfASpCcg9pysR43aoOdmVrRr2QjV6u4/KWdY/0AwqJzXJtCJRN2c04iRpyJEkcpNlIkUY9voCDbIodqGhhUE5PwB

C++

fig.cpp
// C++(gcc)
#include <iostream>
#include <boost/format.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
namespace mp = boost::multiprecision;
using Bint = mp::cpp_int;

int main(void)
{
	string a = "3";
	string b = "3";

	for (int i = 0; i < 20; i++) {
		auto c = Bint(a) * Bint(b);
		cout << (boost::format("%1%: %2% * %3% = %4%") % (i+1) % a % b % c)
			<< endl;
		a = "2" + a;
		b = "4" + b;
	}
}

C#

fig.cs
// C#(.NET Core)
using System;
using System.Numerics;

class Program {
	static void Main(string[] args) {
		var a = "3";
		var b = "3";
		for (var i = 0; i < 20; i++) {
			var c = BigInteger.Parse(a) * BigInteger.Parse(b);
			Console.WriteLine($"{i+1}: {a} * {b} = {c}");
			a = "2"+ a;
			b = "4"+ b;
		}
	}
}

Java

BigInteger:整数
BigDecimal:10進の実数

fig.java
// Java(OpenJDK 8)
import java.math.BigInteger;

public class Main {
	public static void main(String[] args) {
		String a = "3";
		String b = "3";
		for (int i = 0; i < 20; i++) {
			BigInteger c = new BigInteger(a).multiply(new BigInteger(b));
			System.out.printf("%d: %s * %s = %s\n", i+1, a, b, c);
			a = "2" + a;
			b = "4" + b;
		}
	}
}

BigDecimalの例
fig.java
// Java(OpenJDK 8)
import java.math.BigDecimal;

public class Main {
	public static void main(String[] args) {
		String a = "3";
		String b = "3";
		for (int i = 0; i < 20; i++) {
			BigDecimal c = new BigDecimal(a).multiply(new BigDecimal(b));
			System.out.printf("%d: %s * %s = %s\n", i+1, a, b, c);
			a = "2" + a;
			b = "4" + b;
		}
	}
}

https://tio.run/##XY5PS8NAEMXP2U/xCBQSE1OtHsTYi/RUEQ89Wg@TP40bs5uQ3VSK9LPH2UYICMsM7zcz@15NR7puu1LXxdc4LpfYMgjeGGw3L3gIhVRd21vUjBNF9jN5ltWmzKWiJhWiG7JG5sgbMgavJDV@hPcHjSXL7djKAopHwc72UlfvH6C@MqHb9CYEwhr@nZ/OJJvJoe0RSG0hmd2k3J6wcj2Kpk@8ORJy3tHlN2YUUJioobGya07Bv1EWhs7B252MLVXSDjbp2N4eAn9RPGJhcOXKmste@zFb3sagGFmMfLq8JF/5iEAXfcl973Tm9FnwO4tx/AU

0
0
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0