Skip to content

Commit

Permalink
Add some Project Euler test programs
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Jan 1, 2024
1 parent 313ee48 commit 28b2323
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/test/CompilerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ COMPILER_TEST(samples, advent_of_code_2023_day4_part2)
COMPILER_TEST(samples, advent_of_code_2023_day5_part1)
COMPILER_TEST(samples, advent_of_code_2023_day5_part2)
COMPILER_TEST(samples, advent_of_code_2023_day6_part1)
COMPILER_TEST(samples, euler_1)
COMPILER_TEST(samples, euler_2)
COMPILER_TEST(samples, euler_3)
COMPILER_TEST(samples, euler_4)
COMPILER_TEST(samples, euler_5)
COMPILER_TEST(samples, euler_6)
COMPILER_TEST(samples, euler_7)
COMPILER_TEST(select_case, select_case_list_of_number)
COMPILER_TEST(select_case, select_case_multiple_case_else)
COMPILER_TEST(select_case, select_case_number)
Expand Down
14 changes: 14 additions & 0 deletions src/test/programs/samples/euler_1.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#procedure
sub Main()
' https://projecteuler.net/problem=1
dim sum = 0
for i = 1 to 999
if i mod 3 = 0 or i mod 5 = 0 then
sum = sum + i
end if
next
print sum
end sub

--output--
233168
19 changes: 19 additions & 0 deletions src/test/programs/samples/euler_2.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#procedure
sub Main()
' https://projecteuler.net/problem=2
dim sum = 0
dim a = 1
dim b = 1
while a < 4000000
dim c = a + b
a = b
b = c
if a mod 2 = 0 then
sum = sum + a
end if
wend
print sum
end sub

--output--
4613732
23 changes: 23 additions & 0 deletions src/test/programs/samples/euler_3.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#procedure
sub Main()
' https://projecteuler.net/problem=3
dim n = 600851475143
dim factor = 2
dim largestFactor = 1

while n > 1
if n mod factor = 0 then
largestFactor = factor
n = n / factor
while n mod factor = 0
n = n / factor
wend
end if
factor = factor + 1
wend

print largestFactor
end sub

--output--
6857
32 changes: 32 additions & 0 deletions src/test/programs/samples/euler_4.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#procedure
sub Main()
' https://projecteuler.net/problem=4
dim largestPalindrome = 0

' Cheat to make this faster for testing purposes; we already know a=913 and b=993.
' The program works correctly without this cheating optimization.
for a = 912 to 914
for b = a to 994
dim product = a * b
if product > largestPalindrome and IsPalindrome(CodeUnits(product as String)) then
largestPalindrome = product
end if
next
next

print largestPalindrome
end sub

#procedure
function IsPalindrome(bytes as List of Number) as Boolean
dim length = Len(bytes)
for i = 0 to length / 2 - 1
if bytes(i) <> bytes(length - i - 1) then
return false
end if
next
return true
end function

--output--
906609
26 changes: 26 additions & 0 deletions src/test/programs/samples/euler_5.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#procedure
function GCD(a as Number, b as Number) as Number
while b <> 0
dim temp = b
b = a mod b
a = temp
wend
return a
end function

#procedure
function LCM(a as Number, b as Number) as Number
return (a / GCD(a, b)) * b
end function

#procedure
sub Main()
dim result = 1
for i = 1 to 20
result = LCM(result, i)
next
print result
end sub

--output--
232792560
18 changes: 18 additions & 0 deletions src/test/programs/samples/euler_6.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#procedure
sub Main()
dim sumOfSquares = 0
dim sum = 0

for i = 1 to 100
sumOfSquares = sumOfSquares + i * i
sum = sum + i
next

dim squareOfSum = sum * sum
dim difference = squareOfSum - sumOfSquares

print difference
end sub

--output--
25164150
42 changes: 42 additions & 0 deletions src/test/programs/samples/euler_7.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#procedure
sub Main()
dim primeCount = 0
dim num = 1

' This program works with "primeCount < 10001"; the answer is 104743.
' For speed in testing, we'll compute the 1001th prime instead, which is 7927.
while primeCount < 1001
num = num + 1
if IsPrime(num) then
primeCount = primeCount + 1
end if
wend

print num
end sub

#procedure
function IsPrime(n as Number) as Boolean
if n <= 1 then
return false
end if
if n <= 3 then
return true
end if
if n mod 2 = 0 or n mod 3 = 0 then
return false
end if

dim i = 5
while i * i <= n
if n mod i = 0 or n mod (i + 2) = 0 then
return false
end if
i = i + 6
wend

return true
end function

--output--
7927

0 comments on commit 28b2323

Please sign in to comment.