-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some Project Euler test programs
- Loading branch information
Showing
8 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |