2025年12月16日 星期二

使用 Raspberry pi5 解 Project Euler 網頁前十個問題

使用 Raspberry pi5 解 Project Euler 網頁前十個問題


曾慶潭 Ching-Tang Tseng
ilikeforth@gmail.com
Hamilton, New Zealand
16 December 2025


根據 https://projecteuler.net/archives;page=20 網頁,收集於問題彙整檔案(Problem Archives)表內的題目數量,截至今天(20251216)為止,已有 964 個。

這個網頁刊出的題目,偏重於數學問題,而特色就是問題都有點複雜,很多問題的表達方式也非淺顯易懂,需要耐心才能理解。因此,用它門來設計程式求解問題時會有點棘手,卻是磨練思考將數學與程式結合在一起時的最好材料。

這次的貼文展示前十個問題的程式,我採用 ciForth 設計出來的 abcForth 系統求解問題,程式中大量採用 BASIC 程式語法,取其數學表示式最為清楚而這樣設計。展示的目的係因這樣的程式語法與眾不同,別處難以見到,只挑選了前十個來展示,係因我也沒時間只幹此活。

展示程式的部份顯示了我並未為 abcForth 添加字串處裡方面的設計,遇到這方面的問題,仍可以回到 Forth 環境,純粹以 Forth 方式設計出指令,然後透過 BASIC 語法中的RUN 指令執行出結果。這項特色是處裡數學計算問題遭遇仍須處裡字串問題時的解決辦法。問題四與問題八中均展示了這種範例。

問題八是已經貼出過的程式,為求完整,此處重複貼出。

除了軟體的安排外,我刻意轉向 Raspberry pi5 硬體加裝 Ubuntu 24.04 作業系統環境後執行出我為 ciForth 添加的設計,執行測試具有驗證硬體性能的效果。

問題一
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
如果我們列出所有小於 10 且是 3 或 5 的倍數的自然數,我們會得到 3、5、6 和 9。這些倍數的和是 23。
求小於 1000 且是 3 或 5 的倍數的和。

程式與執行結果


 

2 integers i sum

: main
basic
10 let sum = 0
20 for i = 1 to 1000 - 1
30 if ( i mod 3 = 0 ) or ( i mod 5 = 0 ) then 50
40 goto 60 
50 let sum = sum + i 
60 next i
70 print " sum = " ; sum
80 end 
;

main
sum = 233168



問題二
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
斐波那契數列中每一項都是前兩項之和。從 1 和 2 開始,前 10 項為:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
考慮斐波那契數列中數值不超過四百萬的項,求偶數項總和。

程式與執行結果


 

4 integers x y n sum

: main
basic
10 let sum = 0
20 let x = 1
30 let y = 1
40 let n = 0

50 let n = x + y 
60 let y = x
70 let x = n
80 if n mod 2 = 0 then 100
90 goto 110
100 let sum = sum + n
110 if n < 4000000 then -50
120 print " sum = " ; sum 
130 end 
;

main
sum = 4613732



問題三
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
13195 的質因數有 5、7、13 和 29。
600851475143 的最大質因數是多少?

程式與執行結果


 
2 integers i num

: main
basic
10 let num = 600851475143
20 let i = 2
30 if i * i < num then 50
40 goto 100
50 if num mod i = 0 then 70
60 goto 80
70 let num = num / i
80 let i = i + 1
90 goto -30
100 print " num = " ; num
110 end
;

main
num = 6857


問題四
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
回文數正讀反讀都一樣。兩個兩位數相乘所得的最大回文數是 9009 = 91 × 99。
求兩個三位數相乘得到的最大回文數。

程式與執行結果

 
create xy 4 cells allot
create yx 4 cells allot

: str= ( adrA lenA adrB lenB -- flag )      \ compare the strings for equality 
    2 pick <> if  drop drop drop  false exit then 
    tuck + swap  ?do                        \ -- adrA 
        dup c@  I c@  <> if  drop false  unloop exit then 
        1+  loop 
    drop true ; 

: $reverse ( addr len -- )
  2dup + -rot                 	\ -- limit-adr adr cnt 
  2/  over + swap 
  ?do  1-         				\ -- last-adr 
       dup    c@  I c@          \ -- last-adr last-char char 
       2 pick c!  I c! 
  loop 
  drop ; 

4 integers a b x n 

: main 
basic
10 let n = 0
20 for a = 999 to 100 step -1
30 for b = a   to 100 step -1
40 let x = a * b
50 if x > n then 70
60 goto 120
70 run x (.) 2dup xy $! yx $! yx $@ $reverse 
80 if xy $@ yx $@ str= then 100
90 goto 120
100 let n = a * b 
110 print " a = " ; a ; " b = " ; b
120 next b
130 next a
140 print " n = " ; n
150 end 
;

main 
a = 995 b = 583 
a = 993 b = 913 
n = 906609 


問題五
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
2520 是能被 1 到 10 之間的每個數整除而沒有餘數的最小數。
能被 1 到 20 之間的所有數整除的最小正數是多少?

程式與執行結果

 
2 integers n i

: main 
basic
10 let n = 1 
20 for i = 1 to 20
30 let n = lcm ( n i )
40 next i
50 print n
60 end 
;

main
232792560 


問題六
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.
前十個自然數的平方和是:
1^2+ 2^2 + ... + 10^2 = 385
前十個自然數總和的平方是:
(1 + 2 + ... + 10)^2 = 55^2 = 3025
因此,前十個自然數的平方和與該和的平方之差為 3025 − 385 = 2640。
求前一百個自然數的平方和與該和的平方之差。

程式與執行結果

 
3 integers i s1 s2 

: main
basic
10 let s1 = 0 :: s2 = 0 
20 for i = 1 to 100
30 let s1 = s1 + i
40 let s2 = s2 + ( i * i )
50 next i
60 print s1 * s1 - s2
70 end
;

main
25164150


問題七
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
列出前六個質數:2、3、5、7、11 和 13,我們可以看出第 6 個質數是 13。
第 10001 個質數是多少?

程式與執行結果

 
8 integers D N L I S T P count

: test           
basic
10 let p = 0
20 let N = T
30 let D = N mod 2
40 if D = 0 then 220
50 let D = N mod 3
60 if D = 0 then 220

70 let L = sqrt ( N ) + 1
80 for I = 6 to L step 6
90 let D = N mod ( I - 1 )
100 if D = 0 then 210
110 let D = N mod ( I + 1 )
120 if D = 0 then 210
130 next I

140 let P = 1
150 goto 300

210 run 2drop
220 let P = 0
300 end 
;

\ testing is started from 8, jump over 2, 3, 5, 7. 
: main
basic
10 print " Input a specified number S = "
20 inputi S
30 let count = 4 
40 let T = 8

130 run test
140 if P = 1 then 160
150 goto 180
160 let count = count + 1
170 if count = S then 210
180 let T = T + 1 
190 goto -130

210 print N
220 end 
;

main
104743


問題八
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
1000位數中相鄰四個數字的乘積最大,即9 × 9 × 8 × 9 = 5832。
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
找出這個千位數中相鄰的十三個數字,使它們的乘積最大。這個乘積的值是多少?

程式與執行結果

 
: cinumber ( addr -- n )
  0 0  rot dup 1+ c@ [char] . = >r
  $@ r@ if 1 /string then
  >number nip 0 = 
  if d>s r> if negate then 
  else r> drop 2drop 
  then ;

create chr 128 cells allot
create val 128 cells allot
create tmp 2 cells allot

s" 73167176531330624919225119674426574742355349194934" chr $!
s" 96983520312774506326239578318016984801869478851843" chr $+!
s" 85861560789112949495459501737958331952853208805511" chr $+!
s" 12540698747158523863050715693290963295227443043557" chr $+!  <-- Starting from the 198th number
s" 66896648950445244523161731856403098711121722383113" chr $+!
s" 62229893423380308135336276614282806444486645238749" chr $+!
s" 30358907296290491560440772390713810515859307960866" chr $+!
s" 70172427121883998797908792274921901699720888093776" chr $+!
s" 65727333001053367881220235421809751254540594752243" chr $+!
s" 52584907711670556013604839586446706324415722155397" chr $+! 
s" 53697817977846174064955149290862569321978468622482" chr $+!
s" 83972241375657056057490261407972968652414535100474" chr $+!
s" 82166370484403199890008895243450658541227588666881" chr $+!
s" 16427171479924442928230863465674813919123162824586" chr $+!
s" 17866458359124566529476545682848912883142607690042" chr $+!
s" 24219022671055626321111109370544217506941658960408" chr $+!
s" 07198403850962455444362981230987879927244284909188" chr $+!
s" 84580156166097919133875499200524063689912560717606" chr $+!
s" 05886116467109405077541002256983155200055935729725" chr $+!
s" 71636269561882670428252483600823257530420752963450" chr $+!

: chr>val ( -- )
  val 128 cells erase
  chr $@ over + swap 
  do 
     tmp 2 cells erase 
     I c@ tmp $c+
     tmp cinumber val $c+
  loop ;

: mult13 ( add -- n )
  dup c@ swap 1+ dup 12 + swap
  do 
     I c@ * 
  loop ;

variable maxaddr
variable maxval

: 1000calculations ( -- )
  0 maxval !
  val $@ over dup maxaddr ! + swap
  do 
    I mult13 dup maxval @ >
    if   I maxaddr ! maxval !
    else drop
    then
  loop 
;

: check ( -- )
  maxaddr @  val $@ drop - 
  chr $@ drop + 13 type
; 

: main
  chr>val
  1000calculations
  cr ." greatest product 0f 13 adjacent digits are  " check 
  cr ." product value =  " maxval @ . ;

main
greatest product 0f 13 adjacent digits are  5576689664895  (Starting from the 198th number)
product value =  23514624000  OK


問題九
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
勾股數是指一組三個自然數 a < b < c,滿足:
a^2 + b^2 = c^2
例如,3^2 + 4^2 = 9 + 16 = 25 = 5^2。
存在唯一一個勾股數 a + b + c = 1000。
求 abc 的乘積。

程式與執行結果

 
3 integers a b c

: main
  basic
10 for a = 1 to 1000
20 for b = a to 1000
30 let c = 1000 - a - b
40 if ( a * a + b * b ) = ( c * c ) then 60
50 goto 70
60 print " a * b * c = " , a * b * c
70 next b
80 next a 
90 end
;

main
a * b * c =  31875000


問題十
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
小於 10 的所有質數總和為 2 + 3 + 5 + 7 = 17。
求小於兩百萬的所有質數總和。

程式與執行結果

 
8 integers A D N L I S T P 

: test           
basic
10 let p = 0
20 let N = T
30 let D = N mod 2
40 if D = 0 then 220
50 let D = N mod 3
60 if D = 0 then 220

70 let L = sqrt ( N ) + 1
80 for I = 6 to L step 6
90 let D = N mod ( I - 1 )
100 if D = 0 then 210
110 let D = N mod ( I + 1 )
120 if D = 0 then 210
130 next I

140 let P = 1
150 goto 300

210 run 2drop
220 let P = 0
300 end 
;

\ testing is started from 8, jump over 2, 3, 5, 7. 
: main
basic
10 print " Input a specified number S = "
20 inputi S
30 let A = 17
40 let T = 8

130 run test
140 if P = 1 then 160
150 goto 170
160 let A = A + N
170 if T >= S then 210
180 let T = T + 1 
190 goto -130

210 print " Answer = " ; A
220 end 
;


main
Input a specified number S = 
? 2000000

Answer = 142913828922  OK

沒有留言: