1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TypeScript: ピタゴラス数

Last updated at Posted at 2024-12-20

プログラム

pythagoras.ts
console.log("** 開始 ***")
let set_aa = new Set<number>()
const nmax: number = 100
let icount: number = 0
for (let it=1; it<nmax; it+=1)
	{
	const it2 :number = it * it
	for (let jt=it+1; jt<nmax; jt+=1)
		{
		const jt2 :number = jt * jt
		const itjt2 :number = it2 + jt2
		const tt :number  = Math.sqrt(itjt2)
		const kt :number = Math.floor(tt)
		const kt2 :number = kt * kt
		if (itjt2 == kt2)
			{
			const ratio :number = it / jt
			if (!set_aa.has(ratio))
				{
				set_aa.add(ratio)
				console.log(it,jt,kt,ratio)
				icount += 1
				}
			}
		}
	}

console.log("icount = ",icount)
console.log("*** 修了 ***")

実行結果

$ bun run pythagoras.ts 
** 開始 ***
3 4 5 0.75
5 12 13 0.4166666666666667
7 24 25 0.2916666666666667
8 15 17 0.5333333333333333
9 40 41 0.225
11 60 61 0.18333333333333332
12 35 37 0.34285714285714286
13 84 85 0.15476190476190477
16 63 65 0.25396825396825395
20 21 29 0.9523809523809523
20 99 101 0.20202020202020202
28 45 53 0.6222222222222222
33 56 65 0.5892857142857143
36 77 85 0.4675324675324675
39 80 89 0.4875
48 55 73 0.8727272727272727
60 91 109 0.6593406593406593
65 72 97 0.9027777777777778
icount =  18
*** 修了 ***

確認したバージョン

$ node --version
v22.3.0

$ bun --version
1.1.40

tsc でコンパイル

tsc pythagoras.ts --lib es2020,dom

実行

node pythagoras.js
1
0
2

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?