things
							parent
							
								
									48130fdcb3
								
							
						
					
					
						commit
						f678044b9b
					
				
							
								
								
									
										32
									
								
								c/euler.c
								
								
								
								
							
							
						
						
									
										32
									
								
								c/euler.c
								
								
								
								
							|  | @ -1,4 +1,36 @@ | ||||||
| #include <stdio.h> | #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) { | void print_result(int result) { | ||||||
|   printf("%d\n", result); |   printf("%d\n", result); | ||||||
|  |  | ||||||
							
								
								
									
										
											BIN
										
									
								
								c/euler002
								
								
								
								
							
							
						
						
									
										
											BIN
										
									
								
								c/euler002
								
								
								
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -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; | ||||||
|  | } | ||||||
										
											Binary file not shown.
										
									
								
							|  | @ -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); | ||||||
|  | } | ||||||
|  | */ | ||||||
|  | @ -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 | ||||||
										
											Binary file not shown.
										
									
								
							|  | @ -0,0 +1 @@ | ||||||
|  | puts (1..999).select { |n| (n % 3 == 0) || (n % 5 == 0) }.sum | ||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -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 | ||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -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 | ||||||
										
											Binary file not shown.
										
									
								
							|  | @ -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)) | ||||||
|  | @ -57,3 +57,19 @@ | ||||||
| (defun max-of (list) | (defun max-of (list) | ||||||
|   (reduce #'max 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)))))) | ||||||
|  |  | ||||||
|  | @ -1,13 +1,5 @@ | ||||||
| (load "euler.lisp") | (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) | (defun prime-factors (n) | ||||||
|   (select (lambda (a) (eq (mod n a) 0)) (eratosthenes (sqrt n))) |   (select (lambda (a) (eq (mod n a) 0)) (eratosthenes (sqrt n))) | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | @ -0,0 +1,8 @@ | ||||||
|  | (load "euler.lisp") | ||||||
|  | 
 | ||||||
|  | (defun set-of-concatenatable-primes (list) | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | (defun solution () | ||||||
|  |   (combinations 5 (eratosthenes 30000)) | ||||||
|  | ) | ||||||
|  | @ -21,13 +21,7 @@ class Integer | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def factorial |   def factorial | ||||||
|     i = 1 |     (2..self).inject(:*) | ||||||
|     n = self |  | ||||||
|     while (n > 0) |  | ||||||
|       i *= n |  | ||||||
|       n -= 1 |  | ||||||
|     end |  | ||||||
|     i |  | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def to_digit_list |   def to_digit_list | ||||||
|  |  | ||||||
|  | @ -1,13 +1,7 @@ | ||||||
| require_relative 'euler' | require_relative 'euler' | ||||||
| 
 | 
 | ||||||
| def products_of_three_digits | def products_of_three_digits | ||||||
|   [].tap do |products| |   (100..999).to_a.combination(2).map { |p| p.inject(:*) } | ||||||
|     (100..999).each do |i| |  | ||||||
|       (100..999).each do |j| |  | ||||||
|         products << i*j |  | ||||||
|       end |  | ||||||
|     end |  | ||||||
|   end |  | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| def solution | def solution | ||||||
|  |  | ||||||
|  | @ -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 | ||||||
|  | @ -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 | ||||||
		Loading…
	
		Reference in New Issue