master
Evan Hemsley 2018-02-21 13:00:08 -08:00
parent 48130fdcb3
commit f678044b9b
25 changed files with 377 additions and 22 deletions

BIN
c/a.out Executable file

Binary file not shown.

View File

@ -1,4 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct unsigned_int_sized_array {
int size;
int allocated_size;
unsigned int *elements_ptr;
} unsigned_int_sized_array;
/* typedef struct unsigned_int_sized_array thing; */
unsigned_int_sized_array unsigned_int_sized_array_init() {
unsigned_int_sized_array array;
array.size = 0;
array.allocated_size = 16;
unsigned int * elts = malloc(sizeof(unsigned int) * 16);
array.elements_ptr = elts;
return array;
}
void reallocate(unsigned_int_sized_array array) {
array.allocated_size *= 2;
realloc(array.elements_ptr, sizeof(unsigned int) * array.allocated_size);
}
void push(unsigned_int_sized_array array, unsigned int new_elt) {
if (array.size >= array.allocated_size) {
reallocate(array);
}
array.elements_ptr[array.size] = new_elt;
array.size++;
}
void print_result(int result) {
printf("%d\n", result);

Binary file not shown.

BIN
c/euler003 Executable file

Binary file not shown.

35
c/euler003.c Normal file
View File

@ -0,0 +1,35 @@
#include "euler.h"
#include "generic_list.h"
#include <math.h>
#include <stdbool.h>
bool prime(long int n) {
for (long i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
generic_list *factors(long n) {
static generic_list list;
generic_list_init(&list);
for (long i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
if (prime(i)) {
generic_list_add(&list, LONG, &i);
}
}
}
return &list;
}
int main() {
generic_list *result;
result = factors(600851475143);
generic_list_print(result);
return 0;
}

BIN
c/euler003.o Normal file

Binary file not shown.

130
c/generic_list.c Normal file
View File

@ -0,0 +1,130 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "generic_list.h"
void generic_node_init(generic_node *n, GENERIC_LIST_TYPE type, void* value) {
n->data = malloc(sizeof(value));
n->type = type;
memcpy(n->data, value, sizeof(value));
}
void generic_list_init(generic_list *l) {
l->size = 0;
l->head = NULL;
l->tail = NULL;
}
int generic_list_size(generic_list *l) {
return l->size;
}
void generic_list_add(generic_list *l, GENERIC_LIST_TYPE type, void* value) {
generic_node *node = malloc(sizeof(generic_node));
node->data = malloc(sizeof(value));
node->type = type;
node->next = NULL;
memcpy(node->data, value, sizeof(value));
if (l->size == 0) {
l->head = node;
l->tail = node;
} else {
l->tail->next = node;
l->tail = node;
}
l->size++;
}
generic_node generic_list_get(generic_list *l, int index) {
assert(index >= l->size);
int i = 0;
generic_node* current = l->head;
while(i < index) {
current = current->next;
i++;
}
return *current;
}
/*
int generic_list_get(generic_list *l, int index) {
if (index >= l->count) {
printf("error: index out of range");
return 0;
}
return l->data[index];
}
void generic_list_set(generic_list *l, int index, int value) {
if (index >= l->count) {
printf("error: index out of range");
return;
}
l->data[index] = value;
}
void generic_list_delete(generic_list *l, int index) {
if (index >= l->count) {
printf("error: index out of range");
return;
}
int j = index;
for (int i = index; i < l->count; i++) {
l->data[j] = l->data[i];
j++;
}
l->count--;
}
int generic_list_sum(generic_list *l) {
int sum = 0;
for (int i = 0; i < l->count; i++) {
sum += (l->data[i]);
}
return sum;
}
*/
void generic_list_print(generic_list *l) {
generic_node *node = l->head;
while (node != NULL) {
switch(node->type) {
case INT:
{
int value = *(int*)(node->data);
printf("%i ", value);
}
break;
case LONG:
{
long value = *(long*)(node->data);
printf("%li ", value);
}
break;
case LONG_LONG:
{
long long value = *(long long*)(node->data);
printf("%lli ", value);
}
break;
}
node = node->next;
}
printf("\n");
}
/*
void generic_list_free(generic_list *l) {
free(l->data);
}
*/

33
c/generic_list.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef GENERIC_LIST_H_
#define GENERIC_LIST_H_
typedef enum {
INT,
LONG,
LONG_LONG
} GENERIC_LIST_TYPE;
typedef struct generic_node {
void* data;
GENERIC_LIST_TYPE type;
struct generic_node* next;
} generic_node;
typedef struct generic_list_ {
int size;
generic_node *head;
generic_node *tail;
} generic_list;
void generic_node_init(generic_node *n, GENERIC_LIST_TYPE type, void* value);
void generic_list_init(generic_list *l);
int generic_list_count(generic_list *l);
void generic_list_add(generic_list *l, GENERIC_LIST_TYPE type, void* value);
generic_node generic_list_get(generic_list *l, int index);
void generic_list_set(generic_list *l, int index, void* value);
void generic_list_delete(generic_list *l, int index);
long long generic_list_sum(generic_list *l);
void generic_list_print(generic_list *l);
void generic_list_free(generic_list *l);
#endif

BIN
crystal/001/euler001 Executable file

Binary file not shown.

1
crystal/001/euler001.cr Normal file
View File

@ -0,0 +1 @@
puts (1..999).select { |n| (n % 3 == 0) || (n % 5 == 0) }.sum

BIN
crystal/001/euler001.dwarf Normal file

Binary file not shown.

BIN
crystal/002/euler002 Executable file

Binary file not shown.

18
crystal/002/euler002.cr Normal file
View File

@ -0,0 +1,18 @@
def fibonacci_nums_up_to(n)
result = [1] of Int32
a = 1
b = 2
accum = 0
while accum < n
accum = a + b
result << b
a = b
b = accum
end
result
end
puts fibonacci_nums_up_to(4000000).select{ |n| n.even? }.sum

BIN
crystal/002/euler002.dwarf Normal file

Binary file not shown.

BIN
crystal/003/euler003 Executable file

Binary file not shown.

26
crystal/003/euler003.cr Normal file
View File

@ -0,0 +1,26 @@
#wrong approach, should just do recursive trial division...
def eratosthenes_sieve(n)
sieve = Array.new(n + 1, true)
sieve[0] = false
sieve[1] = false
2.step(to: Math.sqrt(n)) do |i|
if sieve[i]
(i * i).step(to: n, by: i) do |j|
sieve[j] = false
end
end
end
result = sieve.map_with_index { |b, i| b ? i : nil }.compact
end
def prime_factors(n)
nums = eratosthenes_sieve(n).select { |i| n % i == 0 }
nums = nums + nums.map { |i| n / i }
end
puts eratosthenes_sieve(600851475143)
puts prime_factors(600851475143).max

BIN
crystal/003/euler003.dwarf Normal file

Binary file not shown.

40
haskell/euler060.hs Normal file
View File

@ -0,0 +1,40 @@
import Data.List (unfoldr)
import Data.List (find)
import Data.Maybe (listToMaybe)
minus (x:xs) (y:ys) = case (compare x y) of
LT -> x : minus xs (y:ys)
EQ -> minus xs ys
GT -> minus (x:xs) ys
minus xs _ = xs
union (x:xs) (y:ys) = case (compare x y) of
LT -> x : union xs (y:ys)
EQ -> x : union xs ys
GT -> y : union (x:xs) ys
union xs [] = xs
union [] ys = ys
primesToQ m = eratos [2..m]
where
eratos [] = []
eratos (p:xs) = p : eratos (xs `minus` [p*p, p*p+p..m])
combinations 0 _ = [[]]
combinations n xs = [ xs !! i : x | i <- [0..(length xs)-1]
, x <- combinations (n-1) (drop (i+1) xs) ]
pfactors prs n = unfoldr (\(ds,n) -> listToMaybe
[(x, (dropWhile (< x) ds, div n x)) | x <- takeWhile ((<=n).(^2)) ds ++
[n|n>1], mod n x==0]) (prs,n)
primes = 2 : 3 : [x | x <- [5,7..], head (pfactors (tail primes) x) == x]
isPrime n = n > 1 &&
foldr (\p r -> p*p > n || ((n `rem` p) /= 0 && r))
True primes
is_concatenatable_pair list = isPrime (read(show(list!!0) ++ show(list!!1))) && isPrime (read(show(list!!1) ++ show(list!!0)))
is_concatenatable_set list = and (map is_concatenatable_pair (combinations 2 list))
solution = find is_concatenatable_set (combinations 4 (primesToQ 30000))

View File

@ -57,3 +57,19 @@
(defun max-of (list)
(reduce #'max list)
)
(defun eratosthenes-helper (expr list-one list-two)
(if (> (length list-one) 0) (eratosthenes-helper expr (filter (partial expr (first list-one)) (rest list-one)) (filter (partial expr (first list-one)) list-two)) list-two)
)
(defun eratosthenes (n)
(eratosthenes-helper (lambda (x a) (and (eq (mod a x) 0) (/= a x))) (range 2 (sqrt n)) (range 2 n))
)
(defun combinations (count list)
(cond
((zerop count) '(())) ; one combination of zero element.
((endp list) '()) ; no combination from noe element.
(t (nconc (mapcar (let ((item (first list))) (lambda (combi) (cons item combi)))
(combinations (1- count) (rest list)))
(combinations count (rest list))))))

View File

@ -1,13 +1,5 @@
(load "euler.lisp")
(defun eratosthenes-helper (expr list-one list-two)
(if (> (length list-one) 0) (eratosthenes-helper expr (filter (partial expr (first list-one)) (rest list-one)) (filter (partial expr (first list-one)) list-two)) list-two)
)
(defun eratosthenes (n)
(eratosthenes-helper (lambda (x a) (and (eq (mod a x) 0) (/= a x))) (range 2 (sqrt n)) (range 2 n))
)
(defun prime-factors (n)
(select (lambda (a) (eq (mod n a) 0)) (eratosthenes (sqrt n)))
)

8
lisp/euler060.lisp Normal file
View File

@ -0,0 +1,8 @@
(load "euler.lisp")
(defun set-of-concatenatable-primes (list)
)
(defun solution ()
(combinations 5 (eratosthenes 30000))
)

View File

@ -21,13 +21,7 @@ class Integer
end
def factorial
i = 1
n = self
while (n > 0)
i *= n
n -= 1
end
i
(2..self).inject(:*)
end
def to_digit_list

View File

@ -1,13 +1,7 @@
require_relative 'euler'
def products_of_three_digits
[].tap do |products|
(100..999).each do |i|
(100..999).each do |j|
products << i*j
end
end
end
(100..999).to_a.combination(2).map { |p| p.inject(:*) }
end
def solution

20
ruby/euler058.rb Normal file
View File

@ -0,0 +1,20 @@
require 'prime'
def solution
diagonal_primes = []
diagonal_composites = []
increment = 2
num = 1
loop do
4.times do
num += increment
if num.prime?
diagonal_primes << num
else
diagonal_composites << num
end
return num if (diagonal_primes.count.to_f / diagonal_composites.count.to_f) < 0.1
end
end
end

16
ruby/euler060.rb Normal file
View File

@ -0,0 +1,16 @@
require_relative 'euler'
def set_of_concatenatable_primes_old?(list)
list.combination(2).map { |q, r| (q.to_s + r.to_s).to_i.prime? and (r.to_s + q.to_s).to_i.prime? }.all?
end
def solution
Prime.take(200).combination(3).find { |set| set_of_concatenatable_primes?(set) }
end
def set_of_concatenatable_primes?(list)
list.combination(2).each do |q, r|
return false unless (q.to_s + r.to_s).to_i.prime? and (r.to_s + q.to_s).to_i.prime?
end
true
end