65 lines
995 B
V
65 lines
995 B
V
module euler
|
|
import math
|
|
|
|
pub fn range(start int, end int) []int {
|
|
mut result := []int
|
|
for i := start; i <= end; i++ {
|
|
result << i
|
|
}
|
|
return result
|
|
}
|
|
|
|
pub fn even(n int) bool {
|
|
return n % 2 == 0
|
|
}
|
|
|
|
pub fn prime(n u64) bool {
|
|
for i := u64(2); i <= u64(math.sqrt(f64(n))); i++ {
|
|
if n % i == 0 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
pub fn factors(n u64) []u64 {
|
|
mut factors := []u64
|
|
for i := u64(2); i <= u64(math.sqrt(f64(n))); i++ {
|
|
if n % i == 0 && prime(i) {
|
|
factors << i
|
|
}
|
|
}
|
|
return factors
|
|
}
|
|
|
|
pub fn max(ints []u64) u64 {
|
|
mut max := u64(0)
|
|
for n in ints {
|
|
if n > max {
|
|
max = n
|
|
}
|
|
}
|
|
return max
|
|
}
|
|
|
|
pub fn two_combinations(ints []int) [][]int {
|
|
mut result := [[]int]
|
|
result = []
|
|
for i := 0; i < ints.len; i++ {
|
|
for j := i + 1; j < ints.len; j++ {
|
|
result << [[ints[i], ints[j]]]
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
pub fn palindrome(n int) bool {
|
|
str := n.str()
|
|
for i := 0; i < str.len; i++ {
|
|
if str[i] != str[str.len-1-i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|