more solutions in V

master
Evan Hemsley 2020-01-08 12:51:45 -08:00
parent 6a0060567f
commit 79e1f45b8e
4 changed files with 152 additions and 0 deletions

View File

@ -64,3 +64,115 @@ pub fn palindrome(n int) bool {
}
return true
}
fn check(n int, p int) []int {
mut result := []int
mut q := n / p
mut r := n % p
mut m := n
for r == 0 {
result << p
m = q
q = m / p
r = m % p
}
return result
}
fn trial_division(n int) []int {
mut factors := []int
factors << check(n, 2)
factors << check(n, 3)
mut p := 5
for p * p <= n {
factors << check(n, p)
p += 2
factors << check(n, p)
p += 4
}
if factors.len == 0 { factors << n }
return factors
}
pub fn prime_factorization(n int) map[string]int {
mut result := map[string]int
factors := trial_division(n)
for f in factors {
result[f.str()] = 0
mut num := n
for num % f == 0 {
result[f.str()] = result[f.str()] + 1
num /= f
}
}
return result
}
pub fn eratosthenes_sieve(n int) []bool {
mut sieve := [true].repeat(n + 1)
sieve[0] = false
sieve[1] = false
for i := 2; i <= int(math.sqrt(n)); i++ {
if (sieve[i]) {
for j := i * i; j <= n; j += i {
sieve[j] = false
}
}
}
return sieve
}
pub fn eratosthenes_sieve_expand(n int, sieve []bool) []bool
{
mut new_sieve := sieve
for i := sieve.len + 1; i <= n + 1; i++ {
new_sieve << true
}
for i := 2; i <= int(math.sqrt(n)); i++ {
if (new_sieve[i]) {
mut start := i * i
if (i * i >= sieve.len) {
start = sieve.len + i - (sieve.len % i)
}
for j := start; j <= n; j += i {
new_sieve[j] = false
}
}
}
return new_sieve
}
pub fn primes_up_to(n int) []int {
return primes_from_sieve(eratosthenes_sieve(n))
}
fn count_primes_from_sieve(sieve []bool) int {
mut result := 0
for b in sieve { if b { result++ } }
return result
}
fn primes_from_sieve(sieve []bool) []int {
mut result := []int
for index, b in sieve {
if b { result << index }
}
return result
}
pub fn find_nth_prime(n int) int {
mut sieve_size := 16
mut sieve := eratosthenes_sieve(sieve_size)
for count_primes_from_sieve(sieve) < n {
sieve_size *= 2
sieve = eratosthenes_sieve_expand(sieve_size, sieve)
}
primes := primes_from_sieve(sieve)
return primes[n - 1]
}

26
v/euler005.v Normal file
View File

@ -0,0 +1,26 @@
import euler
import math
fn factorization_of_integer_divisible_by_all_integers_up_to(n int) map[string]int {
mut result := map[string]int
for i in euler.range(2, n) {
for p, e in euler.prime_factorization(i) {
if !(p in result) || e > result[p] {
result[p] = e
}
}
}
return result
}
fn factors_to_int(factorization map[string]int) int {
mut result := 1
for p, e in factorization {
result *= int(math.pow(p.int(), e))
}
return result
}
fn main() {
println(factors_to_int(factorization_of_integer_divisible_by_all_integers_up_to(20)))
}

9
v/euler006.v Normal file
View File

@ -0,0 +1,9 @@
import euler
fn main() {
range := euler.range(1, 100)
sum := range.reduce(euler.add, 0)
square_of_sum := sum * sum
sum_of_squares := range.map(it * it).reduce(euler.add, 0)
println(square_of_sum - sum_of_squares)
}

5
v/euler007.v Normal file
View File

@ -0,0 +1,5 @@
import euler
fn main() {
println(euler.find_nth_prime(10001))
}