diff --git a/c/a.out b/c/a.out new file mode 100755 index 0000000..190064d Binary files /dev/null and b/c/a.out differ diff --git a/c/euler.c b/c/euler.c index 190cf78..81739e0 100644 --- a/c/euler.c +++ b/c/euler.c @@ -1,4 +1,36 @@ #include +#include +#include + +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); diff --git a/c/euler002 b/c/euler002 index e0cb859..f54c705 100755 Binary files a/c/euler002 and b/c/euler002 differ diff --git a/c/euler003 b/c/euler003 new file mode 100755 index 0000000..2aa9ea9 Binary files /dev/null and b/c/euler003 differ diff --git a/c/euler003.c b/c/euler003.c new file mode 100644 index 0000000..1ea9f81 --- /dev/null +++ b/c/euler003.c @@ -0,0 +1,35 @@ +#include "euler.h" +#include "generic_list.h" +#include +#include + +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; +} diff --git a/c/euler003.o b/c/euler003.o new file mode 100644 index 0000000..8e6520a Binary files /dev/null and b/c/euler003.o differ diff --git a/c/generic_list.c b/c/generic_list.c new file mode 100644 index 0000000..7abae3f --- /dev/null +++ b/c/generic_list.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#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); +} +*/ diff --git a/c/generic_list.h b/c/generic_list.h new file mode 100644 index 0000000..fb00ec0 --- /dev/null +++ b/c/generic_list.h @@ -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 diff --git a/crystal/001/euler001 b/crystal/001/euler001 new file mode 100755 index 0000000..c08b083 Binary files /dev/null and b/crystal/001/euler001 differ diff --git a/crystal/001/euler001.cr b/crystal/001/euler001.cr new file mode 100644 index 0000000..702de76 --- /dev/null +++ b/crystal/001/euler001.cr @@ -0,0 +1 @@ +puts (1..999).select { |n| (n % 3 == 0) || (n % 5 == 0) }.sum diff --git a/crystal/001/euler001.dwarf b/crystal/001/euler001.dwarf new file mode 100644 index 0000000..bb4d2a3 Binary files /dev/null and b/crystal/001/euler001.dwarf differ diff --git a/crystal/002/euler002 b/crystal/002/euler002 new file mode 100755 index 0000000..1a6705c Binary files /dev/null and b/crystal/002/euler002 differ diff --git a/crystal/002/euler002.cr b/crystal/002/euler002.cr new file mode 100644 index 0000000..e4750df --- /dev/null +++ b/crystal/002/euler002.cr @@ -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 diff --git a/crystal/002/euler002.dwarf b/crystal/002/euler002.dwarf new file mode 100644 index 0000000..88fe852 Binary files /dev/null and b/crystal/002/euler002.dwarf differ diff --git a/crystal/003/euler003 b/crystal/003/euler003 new file mode 100755 index 0000000..963c05e Binary files /dev/null and b/crystal/003/euler003 differ diff --git a/crystal/003/euler003.cr b/crystal/003/euler003.cr new file mode 100644 index 0000000..38be85b --- /dev/null +++ b/crystal/003/euler003.cr @@ -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 diff --git a/crystal/003/euler003.dwarf b/crystal/003/euler003.dwarf new file mode 100644 index 0000000..07ba194 Binary files /dev/null and b/crystal/003/euler003.dwarf differ diff --git a/haskell/euler060.hs b/haskell/euler060.hs new file mode 100644 index 0000000..9354976 --- /dev/null +++ b/haskell/euler060.hs @@ -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)) diff --git a/lisp/euler.lisp b/lisp/euler.lisp index 30d2fb5..2f57a7f 100644 --- a/lisp/euler.lisp +++ b/lisp/euler.lisp @@ -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)))))) diff --git a/lisp/euler003.lisp b/lisp/euler003.lisp index 7292da8..c863d8e 100644 --- a/lisp/euler003.lisp +++ b/lisp/euler003.lisp @@ -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))) ) diff --git a/lisp/euler060.lisp b/lisp/euler060.lisp new file mode 100644 index 0000000..091dabc --- /dev/null +++ b/lisp/euler060.lisp @@ -0,0 +1,8 @@ +(load "euler.lisp") + +(defun set-of-concatenatable-primes (list) +) + +(defun solution () + (combinations 5 (eratosthenes 30000)) +) diff --git a/ruby/euler.rb b/ruby/euler.rb index 2f72dd6..fe3ec9f 100644 --- a/ruby/euler.rb +++ b/ruby/euler.rb @@ -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 diff --git a/ruby/euler004.rb b/ruby/euler004.rb index a22d0f3..1f1bd28 100644 --- a/ruby/euler004.rb +++ b/ruby/euler004.rb @@ -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 diff --git a/ruby/euler058.rb b/ruby/euler058.rb new file mode 100644 index 0000000..30f0546 --- /dev/null +++ b/ruby/euler058.rb @@ -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 diff --git a/ruby/euler060.rb b/ruby/euler060.rb new file mode 100644 index 0000000..07b83d7 --- /dev/null +++ b/ruby/euler060.rb @@ -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