Initial commit of problems 1 to 26.
This commit is contained in:
commit
4f42f105d3
|
@ -0,0 +1,14 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int n = atoi(argv[1])-1;
|
||||
int p = n/3;
|
||||
int q = n/5;
|
||||
int r = n/15;
|
||||
int s = 3*((p*(p+1))/2) +
|
||||
5*((q*(q+1))/2) -
|
||||
15*((r*(r+1))/2);
|
||||
printf("%d\n", s);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
long sumOfPrimes = 0;
|
||||
|
||||
struct node {
|
||||
long prime;
|
||||
struct node* next;
|
||||
};
|
||||
|
||||
struct node* head = NULL;
|
||||
struct node* tail = NULL;
|
||||
|
||||
void insertNode (long n) {
|
||||
if (head == NULL) {
|
||||
head = (struct node*) malloc (sizeof (struct node));
|
||||
head->prime = n;
|
||||
head->next = NULL;
|
||||
tail = head;
|
||||
}
|
||||
else {
|
||||
struct node* curr = (struct node*) malloc (sizeof (struct node));
|
||||
curr->prime = n;
|
||||
curr->next = NULL;
|
||||
tail->next = curr;
|
||||
tail = curr;
|
||||
}
|
||||
|
||||
sumOfPrimes += n;
|
||||
}
|
||||
|
||||
bool isPrime (long n) {
|
||||
struct node* curr = head;
|
||||
while (curr != NULL && curr->prime <= sqrt(n)) {
|
||||
if (n % curr->prime == 0)
|
||||
return false;
|
||||
curr = curr->next;
|
||||
}
|
||||
insertNode (n);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
long n = atol (argv[1]);
|
||||
for (long i = 2; i < n; i++) {
|
||||
isPrime (i);
|
||||
}
|
||||
|
||||
printf("%lli\n", sumOfPrimes);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
bool isPrime (long n) {
|
||||
for (long i = 2; i <= sqrt(n); i++) {
|
||||
if (n % i == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
long sumOfPrimes = 0;
|
||||
for (long i = 2; i < 2000000; i++) {
|
||||
if (isPrime(i))
|
||||
sumOfPrimes += i;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
|
||||
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
|
||||
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
|
||||
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
|
||||
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
|
||||
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
|
||||
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
|
||||
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
|
||||
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
|
||||
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
|
||||
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
|
||||
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
|
||||
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
|
||||
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
|
||||
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
|
||||
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
|
||||
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
|
||||
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
|
||||
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
|
||||
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
|
|
|
@ -0,0 +1,35 @@
|
|||
import csv
|
||||
|
||||
table = []
|
||||
with open('11.csv', 'rb') as file:
|
||||
reader = csv.reader(file, delimiter=' ')
|
||||
for row in reader:
|
||||
array = []
|
||||
for entry in row:
|
||||
array.append(int(entry))
|
||||
table.append(array)
|
||||
|
||||
horizLargest = 0
|
||||
vertiLargest = 0
|
||||
for i in range(0, len(table)):
|
||||
for j in range(0, len(table) - 4):
|
||||
horizLargest = max(horizLargest,
|
||||
table[i][j] * table[i][j+1]
|
||||
* table[i][j+2] * table[i][j+3])
|
||||
vertiLargest = max(vertiLargest,
|
||||
table[j][i] * table[j+1][i] * table[j+2][i]
|
||||
* table[j+3][i])
|
||||
|
||||
rightDiagonal = 0
|
||||
for i in range(0, len(table) - 4):
|
||||
for j in range(0, len(table) - 4):
|
||||
rightDiagonal = max(rightDiagonal, table[i][j] * table[i+1][j+1]
|
||||
* table[i+2][j+2] * table[i+3][j+3])
|
||||
|
||||
leftDiagonal = 0
|
||||
for i in range(3, len(table)):
|
||||
for j in range(0, len(table) - 4):
|
||||
leftDiagonal = max(leftDiagonal, table[i][j] * table[i-1][j+1]
|
||||
* table[i-2][j+2] * table[i-3][j+3])
|
||||
|
||||
print max(horizLargest, vertiLargest, rightDiagonal, leftDiagonal)
|
|
@ -0,0 +1,22 @@
|
|||
import math
|
||||
|
||||
def ithTriangularNum(i):
|
||||
return 0.5*i*(i+1)
|
||||
|
||||
def numOfFactors(n):
|
||||
s = int(math.sqrt(n))
|
||||
factors = 0
|
||||
for j in range(1, s):
|
||||
if n % j == 0:
|
||||
factors += 1
|
||||
return factors*2
|
||||
|
||||
factors = 0
|
||||
i = 1
|
||||
n = ithTriangularNum(i)
|
||||
while factors <= 500:
|
||||
i += 1
|
||||
n = ithTriangularNum(i)
|
||||
factors = numOfFactors(n)
|
||||
|
||||
print i, n, factors
|
|
@ -0,0 +1,100 @@
|
|||
37107287533902102798797998220837590246510135740250
|
||||
46376937677490009712648124896970078050417018260538
|
||||
74324986199524741059474233309513058123726617309629
|
||||
91942213363574161572522430563301811072406154908250
|
||||
23067588207539346171171980310421047513778063246676
|
||||
89261670696623633820136378418383684178734361726757
|
||||
28112879812849979408065481931592621691275889832738
|
||||
44274228917432520321923589422876796487670272189318
|
||||
47451445736001306439091167216856844588711603153276
|
||||
70386486105843025439939619828917593665686757934951
|
||||
62176457141856560629502157223196586755079324193331
|
||||
64906352462741904929101432445813822663347944758178
|
||||
92575867718337217661963751590579239728245598838407
|
||||
58203565325359399008402633568948830189458628227828
|
||||
80181199384826282014278194139940567587151170094390
|
||||
35398664372827112653829987240784473053190104293586
|
||||
86515506006295864861532075273371959191420517255829
|
||||
71693888707715466499115593487603532921714970056938
|
||||
54370070576826684624621495650076471787294438377604
|
||||
53282654108756828443191190634694037855217779295145
|
||||
36123272525000296071075082563815656710885258350721
|
||||
45876576172410976447339110607218265236877223636045
|
||||
17423706905851860660448207621209813287860733969412
|
||||
81142660418086830619328460811191061556940512689692
|
||||
51934325451728388641918047049293215058642563049483
|
||||
62467221648435076201727918039944693004732956340691
|
||||
15732444386908125794514089057706229429197107928209
|
||||
55037687525678773091862540744969844508330393682126
|
||||
18336384825330154686196124348767681297534375946515
|
||||
80386287592878490201521685554828717201219257766954
|
||||
78182833757993103614740356856449095527097864797581
|
||||
16726320100436897842553539920931837441497806860984
|
||||
48403098129077791799088218795327364475675590848030
|
||||
87086987551392711854517078544161852424320693150332
|
||||
59959406895756536782107074926966537676326235447210
|
||||
69793950679652694742597709739166693763042633987085
|
||||
41052684708299085211399427365734116182760315001271
|
||||
65378607361501080857009149939512557028198746004375
|
||||
35829035317434717326932123578154982629742552737307
|
||||
94953759765105305946966067683156574377167401875275
|
||||
88902802571733229619176668713819931811048770190271
|
||||
25267680276078003013678680992525463401061632866526
|
||||
36270218540497705585629946580636237993140746255962
|
||||
24074486908231174977792365466257246923322810917141
|
||||
91430288197103288597806669760892938638285025333403
|
||||
34413065578016127815921815005561868836468420090470
|
||||
23053081172816430487623791969842487255036638784583
|
||||
11487696932154902810424020138335124462181441773470
|
||||
63783299490636259666498587618221225225512486764533
|
||||
67720186971698544312419572409913959008952310058822
|
||||
95548255300263520781532296796249481641953868218774
|
||||
76085327132285723110424803456124867697064507995236
|
||||
37774242535411291684276865538926205024910326572967
|
||||
23701913275725675285653248258265463092207058596522
|
||||
29798860272258331913126375147341994889534765745501
|
||||
18495701454879288984856827726077713721403798879715
|
||||
38298203783031473527721580348144513491373226651381
|
||||
34829543829199918180278916522431027392251122869539
|
||||
40957953066405232632538044100059654939159879593635
|
||||
29746152185502371307642255121183693803580388584903
|
||||
41698116222072977186158236678424689157993532961922
|
||||
62467957194401269043877107275048102390895523597457
|
||||
23189706772547915061505504953922979530901129967519
|
||||
86188088225875314529584099251203829009407770775672
|
||||
11306739708304724483816533873502340845647058077308
|
||||
82959174767140363198008187129011875491310547126581
|
||||
97623331044818386269515456334926366572897563400500
|
||||
42846280183517070527831839425882145521227251250327
|
||||
55121603546981200581762165212827652751691296897789
|
||||
32238195734329339946437501907836945765883352399886
|
||||
75506164965184775180738168837861091527357929701337
|
||||
62177842752192623401942399639168044983993173312731
|
||||
32924185707147349566916674687634660915035914677504
|
||||
99518671430235219628894890102423325116913619626622
|
||||
73267460800591547471830798392868535206946944540724
|
||||
76841822524674417161514036427982273348055556214818
|
||||
97142617910342598647204516893989422179826088076852
|
||||
87783646182799346313767754307809363333018982642090
|
||||
10848802521674670883215120185883543223812876952786
|
||||
71329612474782464538636993009049310363619763878039
|
||||
62184073572399794223406235393808339651327408011116
|
||||
66627891981488087797941876876144230030984490851411
|
||||
60661826293682836764744779239180335110989069790714
|
||||
85786944089552990653640447425576083659976645795096
|
||||
66024396409905389607120198219976047599490197230297
|
||||
64913982680032973156037120041377903785566085089252
|
||||
16730939319872750275468906903707539413042652315011
|
||||
94809377245048795150954100921645863754710598436791
|
||||
78639167021187492431995700641917969777599028300699
|
||||
15368713711936614952811305876380278410754449733078
|
||||
40789923115535562561142322423255033685442488917353
|
||||
44889911501440648020369068063960672322193204149535
|
||||
41503128880339536053299340368006977710650566631954
|
||||
81234880673210146739058568557934581403627822703280
|
||||
82616570773948327592232845941706525094512325230608
|
||||
22918802058777319719839450180888072429661980811197
|
||||
77158542502016545090413245809786882778948721859617
|
||||
72107838435069186155435662884062257473692284509516
|
||||
20849603980134001723930671666823555245252804609722
|
||||
53503534226472524250874054075591789781264330331690
|
|
|
@ -0,0 +1,17 @@
|
|||
import csv
|
||||
|
||||
array = []
|
||||
with open('13.csv', 'rb') as file:
|
||||
reader = csv.reader(file)
|
||||
for line in reader:
|
||||
array.append(line[0])
|
||||
|
||||
summa = 0
|
||||
for i in range(0, 50):
|
||||
for j in range(0, 100):
|
||||
summa += int(array[j][i])
|
||||
summa *= 10
|
||||
|
||||
sumstr = str(summa)
|
||||
sumlen = len(sumstr)
|
||||
print sumstr[0:10]
|
|
@ -0,0 +1,21 @@
|
|||
collatz = {}
|
||||
|
||||
def findStoreCollatz(n):
|
||||
if n == 1:
|
||||
collatz[1] = 1
|
||||
elif n not in collatz:
|
||||
if n % 2 == 0:
|
||||
collatz[n] = findStoreCollatz(n/2) + 1
|
||||
else:
|
||||
collatz[n] = findStoreCollatz(3*n + 1) + 1
|
||||
return collatz[n]
|
||||
|
||||
maxCollatzStart = 0
|
||||
maxCollatzValue = 0
|
||||
for i in range(1, 1000000):
|
||||
collatzVal = findStoreCollatz(i)
|
||||
if collatzVal > maxCollatzValue:
|
||||
maxCollatzValue = collatzVal
|
||||
maxCollatzStart = i
|
||||
|
||||
print maxCollatzStart, maxCollatzValue
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
char* num = malloc (sizeof (char) * 1);
|
||||
num[0] = 1;
|
||||
|
||||
int len = 1;
|
||||
for (int i = 1; i <= 1000; i++) {
|
||||
if (num[len - 1] >= 5) {
|
||||
num = realloc (num, sizeof (char) * ++len);
|
||||
num[len - 1] = 0;
|
||||
}
|
||||
|
||||
int carry = 0;
|
||||
for (int j = 0; j < len; j++) {
|
||||
int new_carry = (num[j] * 2) / 10;
|
||||
num[j] = carry + (num[j] * 2) % 10;
|
||||
carry = new_carry;
|
||||
}
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
printf("Length: %d\n", len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("%d", num[len - i - 1]);
|
||||
sum += num[i];
|
||||
}
|
||||
free (num);
|
||||
|
||||
printf("\nSum: %d\n", sum);
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int nums[1001];
|
||||
int hundred = 7;
|
||||
int and = 3;
|
||||
|
||||
void assign_base_nums () {
|
||||
nums[0] = 0; // placeholder
|
||||
nums[1] = 3; // one
|
||||
nums[2] = 3; // two
|
||||
nums[3] = 5; // three
|
||||
nums[4] = 4; // four
|
||||
nums[5] = 4; // five
|
||||
nums[6] = 3; // six
|
||||
nums[7] = 5; // seven
|
||||
nums[8] = 5; // eight
|
||||
nums[9] = 4; // nine
|
||||
|
||||
nums[10] = 3; // ten
|
||||
nums[11] = 6; // eleven
|
||||
nums[12] = 6; // twelve
|
||||
nums[13] = 8; // thirteen
|
||||
nums[14] = 8; // fourteen
|
||||
nums[15] = 7; // fifteen
|
||||
nums[16] = 7; // sixteen
|
||||
nums[17] = 9; // seventeen
|
||||
nums[18] = 8; // eighteen
|
||||
nums[19] = 8; // nineteen
|
||||
|
||||
nums[20] = 6; // twenty
|
||||
nums[30] = 6; // thirty
|
||||
nums[40] = 5; // forty
|
||||
nums[50] = 5; // fifty
|
||||
nums[60] = 5; // sixty
|
||||
nums[70] = 7; // seventy
|
||||
nums[80] = 6; // eighty
|
||||
nums[90] = 6; // ninety
|
||||
|
||||
nums[1000] = 11; // one thousand
|
||||
}
|
||||
|
||||
void assign_composite_nums () {
|
||||
// twenty to ninety-nine
|
||||
for (int i = 2; i <= 9; i++) {
|
||||
for (int j = 1; j <= 9; j++) {
|
||||
nums[10*i + j] = nums[10*i] + nums[j];
|
||||
}
|
||||
}
|
||||
|
||||
// one hundred to nine hundred and ninety-nine
|
||||
for (int i = 1; i <= 9; i++) {
|
||||
nums[100*i] = nums[i] + hundred;
|
||||
for (int j = 1; j <= 99; j++) {
|
||||
nums[100*i + j] = nums[100*i] + and + nums[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sum_all_nums() {
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= 1000; i++) {
|
||||
sum += nums[i];
|
||||
}
|
||||
printf("%d\n", sum);
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
assign_base_nums();
|
||||
assign_composite_nums();
|
||||
sum_all_nums();
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int row01[1] = { 75 };
|
||||
int row02[2] = { 95, 64 };
|
||||
int row03[3] = { 17, 47, 82 };
|
||||
int row04[4] = { 18, 35, 87, 10 };
|
||||
int row05[5] = { 20, 4, 82, 47, 65 };
|
||||
int row06[6] = { 19, 1, 23, 75, 3, 34 };
|
||||
int row07[7] = { 88, 2, 77, 73, 7, 63, 67 };
|
||||
int row08[8] = { 99, 65, 4, 28, 6, 16, 70, 92 };
|
||||
int row09[9] = { 41, 41, 26, 56, 83, 40, 80, 70, 33 };
|
||||
int row10[10] = { 41, 48, 72, 33, 47, 32, 37, 16, 94, 29 };
|
||||
int row11[11] = { 53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14 };
|
||||
int row12[12] = { 70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57 };
|
||||
int row13[13] = { 91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48 };
|
||||
int row14[14] = { 63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31 };
|
||||
int row15[15] = { 4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23 };
|
||||
int* triangle[15] = { row01, row02, row03, row04, row05,
|
||||
row06, row07, row08, row09, row10,
|
||||
row11, row12, row13, row14, row15 };
|
||||
|
||||
int path01[1];
|
||||
int path02[2];
|
||||
int path03[3];
|
||||
int path04[4];
|
||||
int path05[5];
|
||||
int path06[6];
|
||||
int path07[7];
|
||||
int path08[8];
|
||||
int path09[9];
|
||||
int path10[10];
|
||||
int path11[11];
|
||||
int path12[12];
|
||||
int path13[13];
|
||||
int path14[14];
|
||||
int path15[15];
|
||||
int* paths[15] = { path01, path02, path03, path04, path05,
|
||||
path06, path07, path08, path09, path10,
|
||||
path11, path12, path13, path14, path15 };
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
paths[0][0] = triangle[0][0];
|
||||
for (int i = 1; i < 15; i++) {
|
||||
for (int j = 0; j <= i; j++) {
|
||||
if (j == 0)
|
||||
paths[i][j] = triangle[i][j] + paths[i - 1][j];
|
||||
else if (j == i)
|
||||
paths[i][j] = triangle[i][j] + paths[i - 1][j - 1];
|
||||
else {
|
||||
int left = paths[i - 1][j - 1];
|
||||
int right = paths[i - 1][j];
|
||||
if (left > right)
|
||||
paths[i][j] = triangle[i][j] + left;
|
||||
else
|
||||
paths[i][j] = triangle[i][j] + right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Triangle of Path Sums:\n");
|
||||
for (int i = 0; i < 15; i++) {
|
||||
for (int j = 0; j <= i; j++) {
|
||||
printf("%d ", paths[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int max_path_sum = 0;
|
||||
for (int i = 0; i < 15; i++) {
|
||||
if (paths[14][i] > max_path_sum)
|
||||
max_path_sum = paths[14][i];
|
||||
}
|
||||
printf("Maximum Path Sum: %d\n", max_path_sum);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int noleap[12] = { 31, 28, 31, 30, 31, 30,
|
||||
31, 31, 30, 31, 30, 31 };
|
||||
int leap[12] = { 31, 29, 31, 30, 31, 30,
|
||||
31, 31, 30, 31, 30, 31 };
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
int sundays = 0;
|
||||
int monmod = 0;
|
||||
|
||||
for (int y = 1900; y < 2000; y++) {
|
||||
int* months;
|
||||
if (y % 4 == 0 && y % 100 != 0 ||
|
||||
y % 4 == 0 && y % 100 == 0 && y % 400 == 0)
|
||||
months = leap;
|
||||
else
|
||||
months = noleap;
|
||||
|
||||
for (int m = 0; m < 12; m++) {
|
||||
monmod = (monmod + months[m]) % 7;
|
||||
if (monmod == 0 && y != 1900)
|
||||
sundays++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d\n", sundays);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
long prevfib = 1;
|
||||
long currfib = 2;
|
||||
long evenfibs = 2;
|
||||
while (currfib < 4000000) {
|
||||
long fib = currfib + prevfib;
|
||||
prevfib = currfib;
|
||||
currfib = fib;
|
||||
if (currfib % 2 == 0) {
|
||||
evenfibs += currfib;
|
||||
}
|
||||
}
|
||||
printf("%d\n", evenfibs);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
// Using Stirling's approximation,
|
||||
// n! ~ sqrt(2*pi*n) * (n/e)^n
|
||||
// ln(n!) ~ 0.5*ln(2*pi*n) + n*ln(n) - n
|
||||
// So the number of digits of n! is around
|
||||
// log(n!) = ln(n!)/ln(10)
|
||||
// ~ (0.5*ln(2*pi*n) + n*ln(n) - n)/ln(10)
|
||||
|
||||
int getFactorialDigits(double n) {
|
||||
return ceil((0.5*log(2.0*M_PI*n) + n*log(n) - n)/log(10.0));
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
int len = getFactorialDigits(100.0);
|
||||
char* num = calloc (len, sizeof (char));
|
||||
num[0] = 1;
|
||||
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
int carry = 0;
|
||||
for (int j = 0; j < len; j++) {
|
||||
int new_carry = (carry + num[j] * i) / 10;
|
||||
num[j] = (carry + num[j] * i) % 10;
|
||||
carry = new_carry;
|
||||
}
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
sum += num[i];
|
||||
printf("%d", num[i]);
|
||||
}
|
||||
free (num);
|
||||
|
||||
printf("\n%d\n", sum);
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
|
||||
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
|
||||
|
||||
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
|
||||
|
||||
Evaluate the sum of all the amicable numbers under 10000.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int sumOfDivisors[10001];
|
||||
|
||||
void findSumOfProperDivisors () {
|
||||
sumOfDivisors[0] = 0; // placeholder
|
||||
for (int i = 1; i <= 10000; i++) {
|
||||
int sumOfDivisorsOfI = 1;
|
||||
for (int j = 2; j * j <= i; j++) {
|
||||
if (j * j == i)
|
||||
sumOfDivisorsOfI += j;
|
||||
else if (i % j == 0)
|
||||
sumOfDivisorsOfI += j + i / j;
|
||||
}
|
||||
sumOfDivisors[i] = sumOfDivisorsOfI;
|
||||
}
|
||||
}
|
||||
|
||||
int sumOfAmicable () {
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= 10000; i++) {
|
||||
int value = sumOfDivisors[i];
|
||||
if (value <= 10000 && value != i && sumOfDivisors[value] == i) {
|
||||
sum += i;
|
||||
printf("%d, %d\n", i, value);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
findSumOfProperDivisors ();
|
||||
printf ("%d\n", sumOfAmicable ());
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
|
||||
|
||||
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
|
||||
|
||||
What is the total of all the name scores in the file?
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// returns true if str1 comes before str2
|
||||
// or if str1 == str2
|
||||
bool string_compare (char* str1, char* str2) {
|
||||
int index = 0;
|
||||
while (str1[index] != '\0' && str2[index] != '\0') {
|
||||
if (str1[index] < str2[index])
|
||||
return true;
|
||||
else if (str1[index] > str2[index])
|
||||
return false;
|
||||
else
|
||||
index++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// returns the sum of the value of the chars
|
||||
// where a = 1, b = 2, etc.
|
||||
int name_value (char* name) {
|
||||
int value = 0;
|
||||
int index = 0;
|
||||
while (name[index] != '\0') {
|
||||
value += name[index++] - 64;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void insert (char** string_array, char* string, int index) {
|
||||
if (string == NULL) {
|
||||
string_array[index] = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
char* temp = string_array[index];
|
||||
string_array[index++] = string;
|
||||
return insert (string_array, temp, index);
|
||||
}
|
||||
|
||||
void sort_strings (char** in, char** out) {
|
||||
int in_index = 0;
|
||||
while (in[in_index] != NULL) {
|
||||
char* in_string = in[in_index++];
|
||||
int out_index = 0;
|
||||
while (out[out_index] != NULL) {
|
||||
char* out_string = out[out_index];
|
||||
printf("%s, %s\n", in_string, out_string);
|
||||
if (string_compare(in_string, out_string))
|
||||
insert (out, in_string, out_index);
|
||||
else
|
||||
out_index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
char* in[6];
|
||||
in[6] = NULL;
|
||||
char* out[6];
|
||||
out[0] = NULL;
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
in[i - 1] = argv[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
printf("%s\n", in[i]);
|
||||
}
|
||||
|
||||
sort_strings(in, out);
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
printf("%s\n", out[i]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import csv
|
||||
|
||||
sorted_names_list = sorted(list(csv.reader(open('p022_names.txt', 'rb')))[0])
|
||||
print sum(map(lambda name: (sorted_names_list.index(name) + 1) * sum(map(lambda char: ord(char) - 64, name)), sorted_names_list))
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
|
||||
|
||||
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
|
||||
|
||||
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
|
||||
|
||||
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool abundance[28124];
|
||||
|
||||
void findAbundances() {
|
||||
for (int i = 0; i < 12; i++) {
|
||||
abundance[i] = false;
|
||||
}
|
||||
|
||||
for (int i = 12; i < 28124; i++) {
|
||||
int sumOfProperDivisors = 0;
|
||||
for (int j = 1; j < i / 2 + 1; j++) {
|
||||
if (i % j == 0)
|
||||
sumOfProperDivisors += j;
|
||||
}
|
||||
abundance[i] = sumOfProperDivisors > i;
|
||||
}
|
||||
}
|
||||
|
||||
bool isSumOfAbundances(int n) {
|
||||
for (int i = 12; i < n / 2 + 1; i++) {
|
||||
if (abundance[i] && abundance[n - i])
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
findAbundances();
|
||||
|
||||
int sumOfNotSumOfAbundances = 0;
|
||||
for (int i = 1; i < 28124; i++) {
|
||||
if (!isSumOfAbundances(i))
|
||||
sumOfNotSumOfAbundances += i;
|
||||
}
|
||||
|
||||
printf("%d\n", sumOfNotSumOfAbundances);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
def isAbundant(n):
|
||||
proper_divisors = []
|
||||
for i in range(1, n):
|
||||
if n % i == 0:
|
||||
proper_divisors.append(i)
|
||||
return sum(proper_divisors) > n
|
||||
|
||||
abd_arr = []
|
||||
for i in range(1, 30):
|
||||
abd_arr.append(isAbundant(i))
|
||||
|
||||
is_sum = []
|
||||
for i in range(1, 30):
|
||||
for j in range(1, i/2 + 1):
|
||||
if abd_arr[j+1] and abd_arr[i-j+1]:
|
||||
print i, j
|
||||
is_sum.append(i)
|
||||
break
|
||||
|
||||
print is_sum
|
|
@ -0,0 +1,10 @@
|
|||
import math
|
||||
|
||||
permutation = []
|
||||
digits = range(0, 10)
|
||||
position = 999999
|
||||
while (len(digits)):
|
||||
width = math.factorial(len(digits) - 1)
|
||||
permutation.append(digits.pop(position / width))
|
||||
position %= width
|
||||
print int(''.join(map(str, permutation)))
|
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main (int argc, int argv[]) {
|
||||
char* prevfib = calloc (1000, sizeof (int));
|
||||
char* currfib = calloc (1000, sizeof (int));
|
||||
prevfib[0] = 1;
|
||||
currfib[0] = 1;
|
||||
int index = 2;
|
||||
|
||||
while (1) {
|
||||
index++;
|
||||
char* temp = prevfib;
|
||||
prevfib = currfib;
|
||||
currfib = calloc (1000, sizeof (int));
|
||||
|
||||
char carry = 0;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
char sum = temp[i] + prevfib[i] + carry;
|
||||
currfib[i] = sum % 10;
|
||||
carry = sum / 10;
|
||||
}
|
||||
free (temp);
|
||||
|
||||
if (currfib[999] != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
printf ("%d\n", index);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
def numOfRepetends(n):
|
||||
if not n:
|
||||
return 0
|
||||
|
||||
remainders = []
|
||||
dividend = 10
|
||||
while dividend != 0:
|
||||
if dividend in remainders:
|
||||
return len(remainders) - remainders.index(dividend)
|
||||
else:
|
||||
remainders.append(dividend)
|
||||
dividend = (dividend % n) * 10
|
||||
return 0
|
||||
|
||||
repetendLengths = map(numOfRepetends, range(0, 1000))
|
||||
maxRepetend = repetendLengths.index(max(repetendLengths))
|
||||
print maxRepetend
|
|
@ -0,0 +1,13 @@
|
|||
Euler discovered the remarkable quadratic formula:
|
||||
|
||||
n^2+n+41
|
||||
|
||||
It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤39. However, when n=40,40^2+40+41=40(40+1)+41 is divisible by 41, and certainly when n=41,41^2+41+4 is clearly divisible by 41.
|
||||
|
||||
The incredible formula n^2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0≤n≤79. The product of the coefficients, −79 and 1601, is −126479.
|
||||
|
||||
Considering quadratics of the form:
|
||||
|
||||
n^2+an+b, where |a|<1000 and |b|≤1000
|
||||
|
||||
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
long long largestPrimeFactor(long long n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
long long m = sqrt(n);
|
||||
for (long long i = 2; i <= m; i++) {
|
||||
if (n % i == 0) {
|
||||
long long f1 = largestPrimeFactor(i);
|
||||
long long f2 = largestPrimeFactor(n/i);
|
||||
if (f1 > f2)
|
||||
return f1;
|
||||
else
|
||||
return f2;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
long long n = atoll(argv[1]);
|
||||
printf("%d\n", largestPrimeFactor(n));
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int checkIfPalindromic(int n) {
|
||||
int num = n;
|
||||
int rev = 0;
|
||||
while (num > 0) {
|
||||
int deg = num % 10;
|
||||
rev = rev * 10 + deg;
|
||||
num = num / 10;
|
||||
}
|
||||
return n == rev;
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
int maxPalindrome = 0;
|
||||
for (int i = 999; i >= 100; i--) {
|
||||
for (int j = 999; j >= i; j--) {
|
||||
int prod = i*j;
|
||||
if (checkIfPalindromic(prod) &&
|
||||
prod > maxPalindrome) {
|
||||
maxPalindrome = prod;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n", maxPalindrome);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int gcd(int v1, int v2) {
|
||||
int min;
|
||||
if (v1 < v2)
|
||||
min = v1;
|
||||
else
|
||||
min = v2;
|
||||
|
||||
int gcd = 1;
|
||||
for (int i = 2; i <= min; i++) {
|
||||
if (v1 % i == 0 && v2 % i == 0) {
|
||||
gcd = i;
|
||||
}
|
||||
}
|
||||
|
||||
return gcd;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int result = 1;
|
||||
for (int i = 2; i <= 20; i++) {
|
||||
printf("the gcd of %d and %d is %d\n", result, i, gcd(result, i));
|
||||
result = result * (double)i/gcd(result, i);
|
||||
}
|
||||
printf("%d\n", result);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
double n = 100.0;
|
||||
double squareOfSumOfN = 0.25*n*n*(n+1)*(n+1);
|
||||
double sumOfSquareOfN = n*(n+1)*(2*n+1)/6;
|
||||
printf("%d\n", (long)(squareOfSumOfN - sumOfSquareOfN));
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool isPrime(long n) {
|
||||
for (long i = 2; i <= sqrt(n); i++) {
|
||||
if (n % i == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int j = 0;
|
||||
int jthPrime = 0;
|
||||
for (long i = 2; j < 10001; i++) {
|
||||
if (isPrime(i)) {
|
||||
j++;
|
||||
jthPrime = i;
|
||||
// printf("%d: %d\n", j, jthPrime);
|
||||
}
|
||||
}
|
||||
printf("%d\n", jthPrime);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
char* n = argv[1];
|
||||
|
||||
if (strlen(n) < 13)
|
||||
printf("%s\n", "Your number has less than 13 digits, which may cause undefined behaviour.");
|
||||
|
||||
long long largestProduct = 1;
|
||||
for (int k = 0; k < strlen(n) - 12; k++) {
|
||||
long long product = 1;
|
||||
for (int j = 0; j < 13; j++) {
|
||||
product *= (n[j+k]-'0');
|
||||
}
|
||||
if (product > largestProduct) {
|
||||
largestProduct = product;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%lli\n", largestProduct);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
for (int i = 1; i < 334; i++) {
|
||||
for (int j = i; j < (1000 - i)/2 + 1; j++) {
|
||||
int k = 1000 - i - j;
|
||||
if (i*i + j*j == k*k ||
|
||||
i*i + k*k == j*j ||
|
||||
j*j + k*k == i*i)
|
||||
printf("%d, %d, %d, %d\n", i, j, k, i*j*k);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
home
|
||||
t I sigh
|
||||
q
|
|
@ -0,0 +1,2 @@
|
|||
Recovery Key: 1042140-wZ0RF9oumnwwTFibV6vkWDX7Q4P6w9vQ2wg0DPYz
|
||||
Generated: Tue, 8 Nov 2016, 15:17.04
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue