shorter / better solution for PE#7
authorcassowarii <2374677+cassowarii@users.noreply.github.com>
Sun, 19 Jul 2026 04:05:57 +0000 (21:05 -0700)
committercassowarii <2374677+cassowarii@users.noreply.github.com>
Sun, 19 Jul 2026 04:06:38 +0000 (21:06 -0700)
sample/project_euler/pe07.sa
sample/project_euler/pe08.sa

index 2defd66..a87613c 100644 (file)
@@ -1,24 +1,8 @@
-# See Project Euler #3
-def factors num {
-  let list = []
-  let candidate = 2
-  let max = math::sqrt num
-  while candidate <= max {
-    list.push candidate if num %% candidate
-    candidate = candidate + 1
-  }
-  list
-}
-
-def prime? num {
-  factors(num).length() == 0
-}
-
-let seen = 0
 let num = 1
-while seen < 10001 {
+let primes = []
+while primes.length < 10001 {
   num = num + 1
-  seen = seen + 1 if prime? num
+  primes.push num unless primes.any? => p { num %% p }
 }
 
-println num
+println primes[primes.length - 1]
index 855cb15..0485fe6 100644 (file)
@@ -4,5 +4,3 @@ let span = 13
 println list::iota(0, digits.length - span + 1).map(=> index {
   digits[index..index + span].split().map(integer::from).reduce(1, => a, b { a * b })
 }).reduce(0, math::max)
-
-println list::iota(0, digits.length - span + 1).map(| digits[_.._ + span].split().map(integer::from).reduce(1, {*})).reduce(0, math::max)