master
evan hemsley 2019-02-05 03:06:21 -08:00
parent 32ae71cf32
commit fa570c3b3e
5 changed files with 179 additions and 0 deletions

84
wren/euler.wren Normal file
View File

@ -0,0 +1,84 @@
class Prime {
static subfactors(n, p) {
var subfactors = []
var q = n / p
var r = n % p
while (r == 0) {
subfactors.add(p)
n = q
q = n / p
r = n % p
}
return [n, subfactors]
}
static trial_factorization(n) {
var factors = []
var subfactor_result = subfactors(n, 2)
n = subfactor_result[0]
Utility.extend_list(factors, subfactor_result[1])
subfactor_result = subfactors(n, 3)
n = subfactor_result[0]
Utility.extend_list(factors, subfactor_result[1])
var p = 5
while (p * p <= n) {
subfactor_result = subfactors(n, p)
n = subfactor_result[0]
Utility.extend_list(factors, subfactor_result[1])
p = p + 2
subfactor_result = subfactors(n, p)
n = subfactor_result[0]
Utility.extend_list(factors, subfactor_result[1])
p = p + 4
}
if (n >= 1) { factors.add(n) }
return factors
}
static prime_factors(integer) {
return trial_factorization(integer)
}
}
class Utility {
static cons(x, xs) {
return extend_list([x], xs)
}
static extend_list(list_one, list_two) {
for (element in list_two) {
list_one.add(element)
}
return list_one
}
static palindrome(n) {
var result = true
var num = n.toString
for (i in (0..(num.count-1)/2)) {
if (num.codePoints[i] != num.codePoints[num.count-1-i]) {
return false
}
}
return true
}
static max(sequence) {
var max = sequence.take(1).toList[0]
for (element in sequence) {
if (element > max) {
max = element
}
}
return max
}
}

11
wren/euler001.wren Normal file
View File

@ -0,0 +1,11 @@
class Euler001 {
static multiples_of_three_and_five_below(n) {
return (3..n-1).where { |i| (i % 3 == 0) || (i % 5 == 0) }
}
static solution {
return multiples_of_three_and_five_below(1000).reduce { |x,y| x + y }
}
}
System.print(Euler001.solution)

45
wren/euler002.wren Normal file
View File

@ -0,0 +1,45 @@
class FibonacciIterator is Sequence {
construct new() {
_a = 0
_b = 1
}
iterate(value) {
if (value == null) {
_a = 0
_b = 1
return 1
} else {
var temp = _b
_b = _a + _b
_a = temp
return _b
}
}
iteratorValue(value) {
return value
}
until(value) {
var list = []
for (i in this) {
if (i > value) {
break
} else {
list.add(i)
}
}
return list
}
}
class Euler002 {
static solution {
var myFib = FibonacciIterator.new()
return myFib.until(4000000).where { |n| n % 2 == 0 }.reduce { |x, y| x + y }
}
}
System.print(Euler002.solution)

9
wren/euler003.wren Normal file
View File

@ -0,0 +1,9 @@
import "./euler" for Prime
class Euler003 {
static solution {
return Prime.prime_factors(600851475143)[-1]
}
}
System.print(Euler003.solution)

30
wren/euler004.wren Normal file
View File

@ -0,0 +1,30 @@
import "./euler" for Utility
class Euler004 {
static combination(list, k) {
if (k == 0) {
return [[]]
}
if (list.isEmpty) {
return []
}
var x = list[0]
var xs = list.skip(1).toList
return Utility.extend_list(combination(xs, k - 1).map{ |t|
return Utility.extend_list([x], t)
}.toList, combination(xs, k))
}
static products_of_three_digits {
return combination((100..999).toList, 2).map { |pair| pair.reduce { |x, y| x * y }}
}
static solution {
return Utility.max(products_of_three_digits.where { |x| Utility.palindrome(x) })
}
}
System.print(Euler004.solution)