プロジェクト

全般

プロフィール

操作

ホーム - Project Euler

Problem 6

Sum Square Difference

The sum of the squares of the first ten natural numbers is,
$$1^2 + 2^2 + ... + 10^2 = 385.$$
The square of the sum of the first ten natural numbers is,
$$(1 + 2 + ... + 10)^2 = 55^2 = 3025.$$
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is $3025 - 385 = 2640$.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

二乗和の差

最初の10個の自然数について, その二乗の和は,
$$1^2 + 2^2 + ... + 10^2 = 385.$$
最初の10個の自然数について, その和の二乗は,
$$(1 + 2 + ... + 10)^2 = 55^2 = 3025.$$
これらの数の差は 3025 - 385 = 2640 となる.
同様にして, 最初の100個の自然数について二乗の和と和の二乗の差を求めよ.

#!r6rs
#!chezscheme

(import (chezscheme))

(define square-nums
  (map
    (lambda (n) (expt n 2))
    (iota 101)))

(define square-sum
  (apply + square-nums))

(define sum-square
  (expt (apply + (iota 101))
        2))

(define answer-6
  (- sum-square square-sum))

(printf "6: ~D~%" answer-6)

Noppi2023/12/29に更新 · 4件の履歴