diff --git a/examples/puzzles/euler_00.zr b/examples/puzzles/euler_00.zr new file mode 100644 index 0000000..e8805ae --- /dev/null +++ b/examples/puzzles/euler_00.zr @@ -0,0 +1,8 @@ +func main[] : i64 + let sum: i64 = 0 + + for i in 0..266000 + if i % 2 != 0 + sum = sum + i * i + + io.println_i64(sum) \ No newline at end of file diff --git a/examples/puzzles/euler_01.zr b/examples/puzzles/euler_01.zr index 25176e1..6fbe47e 100644 --- a/examples/puzzles/euler_01.zr +++ b/examples/puzzles/euler_01.zr @@ -1,7 +1,7 @@ func main[] : i64 let sum: i64 = 0 - for i in 0..2000000 - if math.is_prime(i) + for i in 0..1000 + if i % 5 == 0 | i % 3 == 0 sum = sum + i io.println_i64(sum) \ No newline at end of file diff --git a/examples/puzzles/euler_02.zr b/examples/puzzles/euler_02.zr index 2b02baa..68bd32c 100644 --- a/examples/puzzles/euler_02.zr +++ b/examples/puzzles/euler_02.zr @@ -1,21 +1,13 @@ -func num_divisors[n: i64] : i64 - let end: i64 = math.isqrt(n) - - let out: i64 = 0 - for i in 1..end+1 - if n % i == 0 - out = out + 2 - - if end * end == n - out = out - 1 - return out - func main[] : i64 - let n: i64 = 0 - let i: i64 = 1 - while true - n = n + i - if num_divisors(n) > 500 - io.println_i64(n) - break - i = i + 1 \ No newline at end of file + let sum: i64 = 0 + let a: i64 = 0 + let b: i64 = 1 + + while a < 4000000 + if a % 2 == 0 + sum = sum + a + let temp: i64 = b + b = a + b + a = temp + + io.println_i64(sum) \ No newline at end of file diff --git a/examples/puzzles/euler_03.zr b/examples/puzzles/euler_03.zr index e1b3084..a4ed5ab 100644 --- a/examples/puzzles/euler_03.zr +++ b/examples/puzzles/euler_03.zr @@ -1,6 +1,11 @@ func main[] : i64 - // leaks a bit of memory but looks very cool - 37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226 - |> str.from_i64() - |> str.substr(0, 10) - |> io.println() \ No newline at end of file + let n: i64 = 600851475143 + let f: i64 = 2 + + while f * f <= n + if n % f == 0 + n = n / f + else + f = f + 1 + + io.println_i64(n) \ No newline at end of file diff --git a/examples/puzzles/euler_04.zr b/examples/puzzles/euler_04.zr index 643d536..3e27193 100644 --- a/examples/puzzles/euler_04.zr +++ b/examples/puzzles/euler_04.zr @@ -1,22 +1,13 @@ -func collatz_num[n: i64] : i64 - if n % 2 == 0 - return n / 2 - return n * 3 + 1 - -func collatz_seq[n: i64]: i64 - let i: i64 = 1 - while n != 1 - n = collatz_num(n) - i = i + 1 - return i - func main[] : i64 - let max: i64 = 0 - let max_index: i64 = 0 + let out: i64 = 0 - for i in 1..1000000 - let seq: i64 = collatz_seq(i) - if seq > max - max = seq - max_index = i - io.println_i64(max_index) \ No newline at end of file + for a in 500..1000 + for b in 500..1000 + if a * b > out + let s: str = str.from_i64(a * b) + let s_rev: str = str.reverse(s) + if str.equal(s, s_rev) + out = a * b + mem.free(s) + mem.free(s_rev) + io.println_i64(out) \ No newline at end of file diff --git a/examples/puzzles/euler_05.zr b/examples/puzzles/euler_05.zr index e2b43f8..312d997 100644 --- a/examples/puzzles/euler_05.zr +++ b/examples/puzzles/euler_05.zr @@ -1,9 +1,6 @@ func main[] : i64 - let n: i64 = 40 - let r: i64 = 20 let out: i64 = 1 - for i in 1..r+1 - out = out * (n - (r - i)) / i - + for i in 1..21 + out = math.lcm(out, i) io.println_i64(out) \ No newline at end of file diff --git a/examples/puzzles/euler_06.zr b/examples/puzzles/euler_06.zr index 6fbe47e..19ab320 100644 --- a/examples/puzzles/euler_06.zr +++ b/examples/puzzles/euler_06.zr @@ -1,7 +1,11 @@ func main[] : i64 - let sum: i64 = 0 + let sum_of_squares: i64 = 0 + for i in 1..101 + sum_of_squares = sum_of_squares + i * i - for i in 0..1000 - if i % 5 == 0 | i % 3 == 0 - sum = sum + i - io.println_i64(sum) \ No newline at end of file + let square_of_sum: i64 = 0 + for i in 1..101 + square_of_sum = square_of_sum + i + square_of_sum = square_of_sum * square_of_sum + + io.println_i64(square_of_sum - sum_of_squares) \ No newline at end of file diff --git a/examples/puzzles/euler_07.zr b/examples/puzzles/euler_07.zr index 68bd32c..6107143 100644 --- a/examples/puzzles/euler_07.zr +++ b/examples/puzzles/euler_07.zr @@ -1,13 +1,11 @@ func main[] : i64 - let sum: i64 = 0 - let a: i64 = 0 - let b: i64 = 1 + let found: i64 = 0 - while a < 4000000 - if a % 2 == 0 - sum = sum + a - let temp: i64 = b - b = a + b - a = temp - - io.println_i64(sum) \ No newline at end of file + let i: i64 = 1 + while true + if math.is_prime(i) + found = found + 1 + if found == 10001 + io.println_i64(i) + break + i = i + 1 \ No newline at end of file diff --git a/examples/puzzles/euler_08.zr b/examples/puzzles/euler_08.zr index a4ed5ab..65e0bba 100644 --- a/examples/puzzles/euler_08.zr +++ b/examples/puzzles/euler_08.zr @@ -1,11 +1,14 @@ func main[] : i64 - let n: i64 = 600851475143 - let f: i64 = 2 + let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" - while f * f <= n - if n % f == 0 - n = n / f - else - f = f + 1 - - io.println_i64(n) \ No newline at end of file + let out: i64 = 0 + let max: i64 = str.len(n) - 13 + for i in 0..max + let s: i64 = 1 + let j: i64 = 0 + while j < 13 + s = s * (n[i + j] - '0') + j = j + 1 + if s > out + out = s + io.println_i64(out) \ No newline at end of file diff --git a/examples/puzzles/euler_09.zr b/examples/puzzles/euler_09.zr index 3e27193..0aaa26b 100644 --- a/examples/puzzles/euler_09.zr +++ b/examples/puzzles/euler_09.zr @@ -1,13 +1,7 @@ func main[] : i64 - let out: i64 = 0 - - for a in 500..1000 - for b in 500..1000 - if a * b > out - let s: str = str.from_i64(a * b) - let s_rev: str = str.reverse(s) - if str.equal(s, s_rev) - out = a * b - mem.free(s) - mem.free(s_rev) - io.println_i64(out) \ No newline at end of file + for a in 1..1000 + for b in 1..1000 + let c: i64 = 1000 - b - a + if a * a + b * b == c * c + io.println_i64(a * b * c) + return 0 \ No newline at end of file diff --git a/examples/puzzles/euler_10.zr b/examples/puzzles/euler_10.zr index 312d997..25176e1 100644 --- a/examples/puzzles/euler_10.zr +++ b/examples/puzzles/euler_10.zr @@ -1,6 +1,7 @@ func main[] : i64 - let out: i64 = 1 + let sum: i64 = 0 - for i in 1..21 - out = math.lcm(out, i) - io.println_i64(out) \ No newline at end of file + for i in 0..2000000 + if math.is_prime(i) + sum = sum + i + io.println_i64(sum) \ No newline at end of file diff --git a/examples/puzzles/euler_11.zr b/examples/puzzles/euler_11.zr deleted file mode 100644 index 19ab320..0000000 --- a/examples/puzzles/euler_11.zr +++ /dev/null @@ -1,11 +0,0 @@ -func main[] : i64 - let sum_of_squares: i64 = 0 - for i in 1..101 - sum_of_squares = sum_of_squares + i * i - - let square_of_sum: i64 = 0 - for i in 1..101 - square_of_sum = square_of_sum + i - square_of_sum = square_of_sum * square_of_sum - - io.println_i64(square_of_sum - sum_of_squares) \ No newline at end of file diff --git a/examples/puzzles/euler_12.zr b/examples/puzzles/euler_12.zr index 6107143..2b02baa 100644 --- a/examples/puzzles/euler_12.zr +++ b/examples/puzzles/euler_12.zr @@ -1,11 +1,21 @@ -func main[] : i64 - let found: i64 = 0 +func num_divisors[n: i64] : i64 + let end: i64 = math.isqrt(n) + let out: i64 = 0 + for i in 1..end+1 + if n % i == 0 + out = out + 2 + + if end * end == n + out = out - 1 + return out + +func main[] : i64 + let n: i64 = 0 let i: i64 = 1 while true - if math.is_prime(i) - found = found + 1 - if found == 10001 - io.println_i64(i) - break + n = n + i + if num_divisors(n) > 500 + io.println_i64(n) + break i = i + 1 \ No newline at end of file diff --git a/examples/puzzles/euler_13.zr b/examples/puzzles/euler_13.zr index 65e0bba..e1b3084 100644 --- a/examples/puzzles/euler_13.zr +++ b/examples/puzzles/euler_13.zr @@ -1,14 +1,6 @@ func main[] : i64 - let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" - - let out: i64 = 0 - let max: i64 = str.len(n) - 13 - for i in 0..max - let s: i64 = 1 - let j: i64 = 0 - while j < 13 - s = s * (n[i + j] - '0') - j = j + 1 - if s > out - out = s - io.println_i64(out) \ No newline at end of file + // leaks a bit of memory but looks very cool + 37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226 + |> str.from_i64() + |> str.substr(0, 10) + |> io.println() \ No newline at end of file diff --git a/examples/puzzles/euler_14.zr b/examples/puzzles/euler_14.zr index 0aaa26b..643d536 100644 --- a/examples/puzzles/euler_14.zr +++ b/examples/puzzles/euler_14.zr @@ -1,7 +1,22 @@ +func collatz_num[n: i64] : i64 + if n % 2 == 0 + return n / 2 + return n * 3 + 1 + +func collatz_seq[n: i64]: i64 + let i: i64 = 1 + while n != 1 + n = collatz_num(n) + i = i + 1 + return i + func main[] : i64 - for a in 1..1000 - for b in 1..1000 - let c: i64 = 1000 - b - a - if a * a + b * b == c * c - io.println_i64(a * b * c) - return 0 \ No newline at end of file + let max: i64 = 0 + let max_index: i64 = 0 + + for i in 1..1000000 + let seq: i64 = collatz_seq(i) + if seq > max + max = seq + max_index = i + io.println_i64(max_index) \ No newline at end of file diff --git a/examples/puzzles/euler_15.zr b/examples/puzzles/euler_15.zr new file mode 100644 index 0000000..e2b43f8 --- /dev/null +++ b/examples/puzzles/euler_15.zr @@ -0,0 +1,9 @@ +func main[] : i64 + let n: i64 = 40 + let r: i64 = 20 + let out: i64 = 1 + + for i in 1..r+1 + out = out * (n - (r - i)) / i + + io.println_i64(out) \ No newline at end of file diff --git a/src/std.zr b/src/std.zr index fce4d57..e48591d 100644 --- a/src/std.zr +++ b/src/std.zr @@ -376,7 +376,7 @@ func array.nth[xs: array, n: i64] : i64 func array.set[xs: array, n: i64, x: i64] : void if n < 0 | n >= array.size(xs) - dbg.panic("array.nth out of bounds") + dbg.panic("array.set out of bounds") let data: ptr = mem.read64(xs) mem.write64(data + n * 8, x)