プロジェクト

全般

プロフィール

Problem 6 » 履歴 » リビジョン 3

リビジョン 2 (Noppi, 2023/12/27 04:47) → リビジョン 3/4 (Noppi, 2023/12/27 13:18)

[ホーム](https://redmine.noppi.jp) - [[Wiki|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個の自然数について二乗の和と和の二乗の差を求めよ. 

 ```scheme 
 #!r6rs 
 #!chezscheme 

 (import (chezscheme)) 

 (define square-num-list answer-6 
   (map (let loop ([n 1] [sum-all 0] [squared-all 0]) 
     (lambda (n) (* (if (< 100 n) 
       (- (expt sum-all 2) 
          squared-all) 
       (loop (add1 n) (+ sum-all n) (+ squared-all 
                                       (expt n n)) 
     (iota 101))) 

 (define square-sum 
   (apply + square-num-list)) 

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

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

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