Initial commit
This commit is contained in:
commit
ceaf6f24eb
52 changed files with 2295 additions and 0 deletions
17
C/1-a.c
Executable file
17
C/1-a.c
Executable file
|
@ -0,0 +1,17 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
|
||||
|
||||
// Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
void main() {
|
||||
int sum = 0;
|
||||
int i = 0;
|
||||
for(i = 1; i < 1000; i++) {
|
||||
if(i % 3 == 0 || i % 5 == 0) {
|
||||
printf("%d, ", i);
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\nSum: %d", sum);
|
||||
}
|
41
C/10-a.c
Executable file
41
C/10-a.c
Executable file
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
// The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
// Find the sum of all the primes below two million.
|
||||
|
||||
int main() {
|
||||
long long sum = 0;
|
||||
int primes = 0;
|
||||
|
||||
int i = 2;
|
||||
|
||||
while(i < 2000000) {
|
||||
if(isPrime(i) == 1) {
|
||||
sum += i;
|
||||
primes++;
|
||||
}
|
||||
|
||||
if(i == 2) {
|
||||
i = 3;
|
||||
} else {
|
||||
i = i + 2;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d primes sum: '%lld'\n\n", primes, sum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int isPrime(int number) {
|
||||
int i;
|
||||
for(i = 3; i < (int)sqrt(number) + 1; i = i + 2) {
|
||||
if(number % i == 0 && i != number) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
57
C/10-b.c
Executable file
57
C/10-b.c
Executable file
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
// The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
// Find the sum of all the primes below two million.
|
||||
|
||||
#define TABLE 100000
|
||||
|
||||
int isPrime(int, int, int*);
|
||||
|
||||
int main() {
|
||||
long long sum = 0;
|
||||
int primesCount = 0, totalPrimes = 0;
|
||||
int primes[TABLE] = {0};
|
||||
|
||||
int i = 2;
|
||||
|
||||
while(i < 20000000) {
|
||||
if(isPrime(i, primesCount, primes) == 1) {
|
||||
if(primesCount < TABLE) {
|
||||
primes[primesCount++] = i;
|
||||
}
|
||||
sum += i;
|
||||
totalPrimes++;
|
||||
}
|
||||
|
||||
if(i == 2) {
|
||||
i = 3;
|
||||
} else {
|
||||
i = i + 2;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d primes sum: '%lld'\n\n", totalPrimes, sum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int isPrime(int number, int primesCount, int *primes) {
|
||||
int i;
|
||||
for(i = 0; i < primesCount && primes[i] * primes[i] <= number; i++) {
|
||||
if(number % primes[i] == 0 && number != primes[i]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(i > 0) {
|
||||
for(i = (primes[i - 1] | 1); i * i <= number; i = i + 2) {
|
||||
if(number % i == 0 && i != number) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
};
|
29
C/10-c.c
Executable file
29
C/10-c.c
Executable file
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define LENGTH 1000
|
||||
|
||||
void main() {
|
||||
long long sum = 0;
|
||||
int numbers[LENGTH + 1] = {0};
|
||||
int i = 1, j;
|
||||
|
||||
while(i < LENGTH) {
|
||||
if(numbers[i] == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for(j = 1; i * j < LENGTH; j++) {
|
||||
numbers[i * j] = 0;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
for(i = 1; i < LENGTH; i++) {
|
||||
if(numbers[i] == 0) {
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("SUM: %lld", sum);
|
||||
}
|
46
C/10-chira.c
Executable file
46
C/10-chira.c
Executable file
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
// Find the sum of all the primes below two million.
|
||||
|
||||
#define MAX_PRIMES 100000
|
||||
|
||||
int isPrime(int, int, int*);
|
||||
|
||||
int main() {
|
||||
long long sum = 2;
|
||||
int primesCount = 0, ct = 0;
|
||||
int primes[MAX_PRIMES];
|
||||
|
||||
int i = 3;
|
||||
|
||||
while(i < 20000000) {
|
||||
if(isPrime(i, primesCount, primes) == 1) {
|
||||
if (primesCount < MAX_PRIMES)
|
||||
primes[primesCount++] = i;
|
||||
sum += i;
|
||||
ct++;
|
||||
}
|
||||
|
||||
i = i + 2;
|
||||
}
|
||||
|
||||
printf("%d primes sum: '%lld'\n", ct, sum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int isPrime(int number, int primesCount, int *primes) {
|
||||
int i;
|
||||
for(i = 0; i < primesCount && primes[i]*primes[i] <= number; i++)
|
||||
if(number % primes[i] == 0)
|
||||
return 0;
|
||||
|
||||
if (i > 0)
|
||||
for(i = (primes[i-1]|1); i*i <= number; i = i + 2)
|
||||
if(number % i == 0)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
31
C/10-lindoc.c
Executable file
31
C/10-lindoc.c
Executable file
|
@ -0,0 +1,31 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void main (void){
|
||||
|
||||
long long topValue = 2000000;
|
||||
int array[topValue+1];
|
||||
long long sum = 2;
|
||||
|
||||
// all number are prime
|
||||
for(long long i=3; i <= topValue;i += 2 ){
|
||||
array[i] = 1;
|
||||
}
|
||||
|
||||
for(long long prime = 3; prime <= topValue;prime += 2){
|
||||
if (array[prime]){
|
||||
for(long long multiple = prime * prime; multiple <= topValue; multiple += prime){
|
||||
if(array[multiple])
|
||||
array[multiple] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (long long i = 3; i <= topValue; i += 2){
|
||||
if(array[i]){
|
||||
sum += i;
|
||||
|
||||
}
|
||||
}
|
||||
printf("Sum is %lld\n", sum);
|
||||
}
|
20
C/11-MyrddinE.html
Executable file
20
C/11-MyrddinE.html
Executable file
|
@ -0,0 +1,20 @@
|
|||
<table><tr><td><font color="#ffffff">08</font></td><td><font color="#ffffff">02</font></td><td><font color="#f8f8f8">22</font></td><td><font color="#e0e0e0">97</font></td><td><font color="#ededed">38</font></td><td><font color="#fbfbfb">15</font></td><td><font color="#ffffff">0</font></td><td><font color="#f9f9f9">40</font></td><td><font color="#ffffff">0</font></td><td><font color="#f3f3f3">75</font></td><td><font color="#fefefe">04</font></td><td><font color="#fdfdfd">05</font></td><td><font color="#fbfbfb">07</font></td><td><font color="#c0c0c0">78</font></td><td><font color="#d5d5d5">52</font></td><td><font color="#f6f6f6">12</font></td><td><font color="#d7d7d7">50</font></td><td><font color="#cecece">77</font></td><td><font color="#d3d3d3">91</font></td><td><font color="#fdfdfd">08</font></td></tr>
|
||||
<tr><td><font color="#f8f8f8">49</font></td><td><font color="#e8e8e8">49</font></td><td><font color="#bfbfbf">99</font></td><td><font color="#d9d9d9">40</font></td><td><font color="#eaeaea">17</font></td><td><font color="#a4a4a4">81</font></td><td><font color="#ebebeb">18</font></td><td><font color="#c8c8c8">57</font></td><td><font color="#bcbcbc">60</font></td><td><font color="#9d9d9d">87</font></td><td><font color="#eaeaea">17</font></td><td><font color="#cccccc">40</font></td><td><font color="#818181">98</font></td><td><font color="#c8c8c8">43</font></td><td><font color="#a6a6a6">69</font></td><td><font color="#c1c1c1">48</font></td><td><font color="#fbfbfb">04</font></td><td><font color="#d2d2d2">56</font></td><td><font color="#d7d7d7">62</font></td><td><font color="#ffffff">0</font></td></tr>
|
||||
<tr><td><font color="#e5e5e5">81</font></td><td><font color="#e0e0e0">49</font></td><td><font color="#dcdcdc">31</font></td><td><font color="#959595">73</font></td><td><font color="#a7a7a7">55</font></td><td><font color="#737373">79</font></td><td><font color="#ebebeb">14</font></td><td><font color="#cccccc">29</font></td><td><font color="#787878">93</font></td><td><font color="#818181">71</font></td><td><font color="#bfbfbf">40</font></td><td><font color="#888888">67</font></td><td><font color="#a1a1a1">53</font></td><td><font color="#636363">88</font></td><td><font color="#cacaca">30</font></td><td><font color="#fafafa">03</font></td><td><font color="#a8a8a8">49</font></td><td><font color="#ededed">13</font></td><td><font color="#e2e2e2">36</font></td><td><font color="#eaeaea">65</font></td></tr>
|
||||
<tr><td><font color="#e6e6e6">52</font></td><td><font color="#bcbcbc">70</font></td><td><font color="#757575">95</font></td><td><font color="#d7d7d7">23</font></td><td><font color="#f6f6f6">04</font></td><td><font color="#818181">60</font></td><td><font color="#e8e8e8">11</font></td><td><font color="#a0a0a0">42</font></td><td><font color="#6f6f6f">69</font></td><td><font color="#cdcdcd">24</font></td><td><font color="#666666">68</font></td><td><font color="#8a8a8a">56</font></td><td><font color="#fdfdfd">01</font></td><td><font color="#b7b7b7">32</font></td><td><font color="#818181">56</font></td><td><font color="#5f5f5f">71</font></td><td><font color="#acacac">37</font></td><td><font color="#fcfcfc">02</font></td><td><font color="#d1d1d1">36</font></td><td><font color="#d3d3d3">91</font></td></tr>
|
||||
<tr><td><font color="#f1f1f1">22</font></td><td><font color="#dcdcdc">31</font></td><td><font color="#e6e6e6">16</font></td><td><font color="#6a6a6a">71</font></td><td><font color="#7c7c7c">51</font></td><td><font color="#525252">67</font></td><td><font color="#676767">63</font></td><td><font color="#191919">89</font></td><td><font color="#9c9c9c">41</font></td><td><font color="#121212">92</font></td><td><font color="#a8a8a8">36</font></td><td><font color="#747474">54</font></td><td><font color="#cacaca">22</font></td><td><font color="#989898">40</font></td><td><font color="#989898">40</font></td><td><font color="#b7b7b7">28</font></td><td><font color="#5f5f5f">66</font></td><td><font color="#bababa">33</font></td><td><font color="#eaeaea">13</font></td><td><font color="#bfbfbf">80</font></td></tr>
|
||||
<tr><td><font color="#f0f0f0">24</font></td><td><font color="#cacaca">47</font></td><td><font color="#cccccc">32</font></td><td><font color="#818181">60</font></td><td><font color="#000000">99</font></td><td><font color="#f8f8f8">03</font></td><td><font color="#8b8b8b">45</font></td><td><font color="#fafafa">02</font></td><td><font color="#8e8e8e">44</font></td><td><font color="#4a4a4a">75</font></td><td><font color="#aaaaaa">33</font></td><td><font color="#767676">53</font></td><td><font color="#363636">78</font></td><td><font color="#a2a2a2">36</font></td><td><font color="#262626">84</font></td><td><font color="#cccccc">20</font></td><td><font color="#ababab">35</font></td><td><font color="#dcdcdc">17</font></td><td><font color="#ececec">12</font></td><td><font color="#cfcfcf">50</font></td></tr>
|
||||
<tr><td><font color="#ebebeb">32</font></td><td><font color="#919191">98</font></td><td><font color="#8a8a8a">81</font></td><td><font color="#c5c5c5">28</font></td><td><font color="#5a5a5a">64</font></td><td><font color="#c8c8c8">23</font></td><td><font color="#525252">67</font></td><td><font color="#e6e6e6">10</font></td><td><font color="#c5c5c5">26</font></td><td><font color="#a3a3a3">38</font></td><td><font color="#989898">40</font></td><td><font color="#525252">67</font></td><td><font color="#707070">59</font></td><td><font color="#747474">54</font></td><td><font color="#4a4a4a">70</font></td><td><font color="#6a6a6a">66</font></td><td><font color="#d1d1d1">18</font></td><td><font color="#b0b0b0">38</font></td><td><font color="#989898">64</font></td><td><font color="#b0b0b0">70</font></td></tr>
|
||||
<tr><td><font color="#d4d4d4">67</font></td><td><font color="#e2e2e2">26</font></td><td><font color="#dfdfdf">20</font></td><td><font color="#878787">68</font></td><td><font color="#fafafa">02</font></td><td><font color="#737373">62</font></td><td><font color="#e0e0e0">12</font></td><td><font color="#d5d5d5">20</font></td><td><font color="#000000">95</font></td><td><font color="#5d5d5d">63</font></td><td><font color="#2b2b2b">94</font></td><td><font color="#9b9b9b">39</font></td><td><font color="#717171">63</font></td><td><font color="#ebebeb">08</font></td><td><font color="#ababab">40</font></td><td><font color="#141414">91</font></td><td><font color="#5f5f5f">66</font></td><td><font color="#999999">49</font></td><td><font color="#686868">94</font></td><td><font color="#e8e8e8">21</font></td></tr>
|
||||
<tr><td><font color="#f0f0f0">24</font></td><td><font color="#c1c1c1">55</font></td><td><font color="#a2a2a2">58</font></td><td><font color="#f5f5f5">05</font></td><td><font color="#757575">66</font></td><td><font color="#666666">73</font></td><td><font color="#3f3f3f">99</font></td><td><font color="#bcbcbc">26</font></td><td><font color="#000000">97</font></td><td><font color="#d6d6d6">17</font></td><td><font color="#363636">78</font></td><td><font color="#5c5c5c">78</font></td><td><font color="#363636">96</font></td><td><font color="#5f5f5f">83</font></td><td><font color="#dbdbdb">14</font></td><td><font color="#383838">88</font></td><td><font color="#a8a8a8">34</font></td><td><font color="#454545">89</font></td><td><font color="#9a9a9a">63</font></td><td><font color="#aeaeae">72</font></td></tr>
|
||||
<tr><td><font color="#f2f2f2">21</font></td><td><font color="#d7d7d7">36</font></td><td><font color="#e2e2e2">23</font></td><td><font color="#efefef">09</font></td><td><font color="#626262">75</font></td><td><font color="#ffffff">0</font></td><td><font color="#6c6c6c">76</font></td><td><font color="#a3a3a3">44</font></td><td><font color="#d2d2d2">20</font></td><td><font color="#9a9a9a">45</font></td><td><font color="#bcbcbc">35</font></td><td><font color="#e2e2e2">14</font></td><td><font color="#ffffff">0</font></td><td><font color="#898989">61</font></td><td><font color="#cacaca">33</font></td><td><font color="#242424">97</font></td><td><font color="#adadad">34</font></td><td><font color="#bebebe">31</font></td><td><font color="#cacaca">33</font></td><td><font color="#949494">95</font></td></tr>
|
||||
<tr><td><font color="#cdcdcd">78</font></td><td><font color="#ececec">17</font></td><td><font color="#bbbbbb">53</font></td><td><font color="#c5c5c5">28</font></td><td><font color="#d5d5d5">22</font></td><td><font color="#6e6e6e">75</font></td><td><font color="#c3c3c3">31</font></td><td><font color="#525252">67</font></td><td><font color="#d9d9d9">15</font></td><td><font color="#000000">94</font></td><td><font color="#f8f8f8">03</font></td><td><font color="#7e7e7e">80</font></td><td><font color="#f8f8f8">04</font></td><td><font color="#afafaf">62</font></td><td><font color="#d6d6d6">16</font></td><td><font color="#dbdbdb">14</font></td><td><font color="#e8e8e8">09</font></td><td><font color="#909090">53</font></td><td><font color="#a5a5a5">56</font></td><td><font color="#979797">92</font></td></tr>
|
||||
<tr><td><font color="#f5f5f5">16</font></td><td><font color="#dadada">39</font></td><td><font color="#fafafa">05</font></td><td><font color="#bcbcbc">42</font></td><td><font color="#000000">96</font></td><td><font color="#b6b6b6">35</font></td><td><font color="#afafaf">31</font></td><td><font color="#9d9d9d">47</font></td><td><font color="#717171">55</font></td><td><font color="#737373">58</font></td><td><font color="#555555">88</font></td><td><font color="#cdcdcd">24</font></td><td><font color="#ffffff">0</font></td><td><font color="#dfdfdf">17</font></td><td><font color="#a8a8a8">54</font></td><td><font color="#c9c9c9">24</font></td><td><font color="#a8a8a8">36</font></td><td><font color="#c3c3c3">29</font></td><td><font color="#767676">85</font></td><td><font color="#bfbfbf">57</font></td></tr>
|
||||
<tr><td><font color="#c8c8c8">86</font></td><td><font color="#c9c9c9">56</font></td><td><font color="#ffffff">0</font></td><td><font color="#b2b2b2">48</font></td><td><font color="#b6b6b6">35</font></td><td><font color="#767676">71</font></td><td bgcolor="#ffeeee"><font color="#282828">89</font></td><td><font color="#ededed">07</font></td><td><font color="#f4f4f4">05</font></td><td><font color="#959595">44</font></td><td><font color="#8e8e8e">44</font></td><td><font color="#b2b2b2">37</font></td><td><font color="#aaaaaa">44</font></td><td><font color="#8b8b8b">60</font></td><td><font color="#c9c9c9">21</font></td><td><font color="#7c7c7c">58</font></td><td><font color="#7c7c7c">51</font></td><td><font color="#8e8e8e">54</font></td><td><font color="#e4e4e4">17</font></td><td><font color="#bebebe">58</font></td></tr>
|
||||
<tr><td><font color="#f3f3f3">19</font></td><td><font color="#b2b2b2">80</font></td><td><font color="#b1b1b1">81</font></td><td><font color="#929292">68</font></td><td><font color="#f3f3f3">05</font></td><td bgcolor="#ffeeee"><font color="#1c1c1c">94</font></td><td><font color="#868686">47</font></td><td><font color="#4d4d4d">69</font></td><td><font color="#b7b7b7">28</font></td><td><font color="#4f4f4f">73</font></td><td><font color="#2f2f2f">92</font></td><td><font color="#dedede">13</font></td><td><font color="#4b4b4b">86</font></td><td><font color="#797979">52</font></td><td><font color="#dcdcdc">17</font></td><td><font color="#383838">77</font></td><td><font color="#f6f6f6">04</font></td><td><font color="#454545">89</font></td><td><font color="#a7a7a7">55</font></td><td><font color="#d2d2d2">40</font></td></tr>
|
||||
<tr><td><font color="#fdfdfd">04</font></td><td><font color="#c5c5c5">52</font></td><td><font color="#f6f6f6">08</font></td><td><font color="#515151">83</font></td><td bgcolor="#ffeeee"><font color="#343434">97</font></td><td><font color="#a5a5a5">35</font></td><td><font color="#000000">99</font></td><td><font color="#d6d6d6">16</font></td><td><font color="#ededed">07</font></td><td><font color="#141414">97</font></td><td><font color="#6c6c6c">57</font></td><td><font color="#adadad">32</font></td><td><font color="#dbdbdb">16</font></td><td><font color="#bcbcbc">26</font></td><td><font color="#bcbcbc">26</font></td><td><font color="#4d4d4d">79</font></td><td><font color="#aaaaaa">33</font></td><td><font color="#c7c7c7">27</font></td><td><font color="#616161">98</font></td><td><font color="#b5b5b5">66</font></td></tr>
|
||||
<tr><td><font color="#c7c7c7">88</font></td><td><font color="#d7d7d7">36</font></td><td><font color="#a8a8a8">68</font></td><td bgcolor="#ffeeee"><font color="#494949">87</font></td><td><font color="#6c6c6c">57</font></td><td><font color="#737373">62</font></td><td><font color="#cccccc">20</font></td><td><font color="#454545">72</font></td><td><font color="#f8f8f8">03</font></td><td><font color="#898989">46</font></td><td><font color="#aaaaaa">33</font></td><td><font color="#525252">67</font></td><td><font color="#909090">46</font></td><td><font color="#717171">55</font></td><td><font color="#e0e0e0">12</font></td><td><font color="#adadad">32</font></td><td><font color="#676767">63</font></td><td><font color="#3c3c3c">93</font></td><td><font color="#aaaaaa">53</font></td><td><font color="#b1b1b1">69</font></td></tr>
|
||||
<tr><td><font color="#fdfdfd">04</font></td><td><font color="#d0d0d0">42</font></td><td><font color="#e8e8e8">16</font></td><td><font color="#666666">73</font></td><td><font color="#a3a3a3">38</font></td><td><font color="#c3c3c3">25</font></td><td><font color="#a7a7a7">39</font></td><td><font color="#e5e5e5">11</font></td><td><font color="#c5c5c5">24</font></td><td><font color="#1c1c1c">94</font></td><td><font color="#515151">72</font></td><td><font color="#d4d4d4">18</font></td><td><font color="#ececec">08</font></td><td><font color="#909090">46</font></td><td><font color="#b9b9b9">29</font></td><td><font color="#b2b2b2">32</font></td><td><font color="#9f9f9f">40</font></td><td><font color="#878787">62</font></td><td><font color="#919191">76</font></td><td><font color="#dddddd">36</font></td></tr>
|
||||
<tr><td><font color="#f6f6f6">20</font></td><td><font color="#c8c8c8">69</font></td><td><font color="#d1d1d1">36</font></td><td><font color="#bdbdbd">41</font></td><td><font color="#747474">72</font></td><td><font color="#c5c5c5">30</font></td><td><font color="#d3d3d3">23</font></td><td><font color="#555555">88</font></td><td><font color="#bebebe">34</font></td><td><font color="#878787">62</font></td><td><font color="#3f3f3f">99</font></td><td><font color="#7a7a7a">69</font></td><td><font color="#606060">82</font></td><td><font color="#7e7e7e">67</font></td><td><font color="#8d8d8d">59</font></td><td><font color="#5b5b5b">85</font></td><td><font color="#707070">74</font></td><td><font color="#f9f9f9">04</font></td><td><font color="#d7d7d7">36</font></td><td><font color="#f3f3f3">16</font></td></tr>
|
||||
<tr><td><font color="#f9f9f9">20</font></td><td><font color="#d0d0d0">73</font></td><td><font color="#e3e3e3">35</font></td><td><font color="#dfdfdf">29</font></td><td><font color="#8e8e8e">78</font></td><td><font color="#d2d2d2">31</font></td><td><font color="#7d7d7d">90</font></td><td><font color="#fefefe">01</font></td><td><font color="#949494">74</font></td><td><font color="#d2d2d2">31</font></td><td><font color="#b8b8b8">49</font></td><td><font color="#989898">71</font></td><td><font color="#bababa">48</font></td><td><font color="#828282">86</font></td><td><font color="#8a8a8a">81</font></td><td><font color="#e8e8e8">16</font></td><td><font color="#dedede">23</font></td><td><font color="#bfbfbf">57</font></td><td><font color="#fbfbfb">05</font></td><td><font color="#dddddd">54</font></td></tr>
|
||||
<tr><td><font color="#ffffff">01</font></td><td><font color="#e9e9e9">70</font></td><td><font color="#e5e5e5">54</font></td><td><font color="#d2d2d2">71</font></td><td><font color="#afafaf">83</font></td><td><font color="#cecece">51</font></td><td><font color="#cbcbcb">54</font></td><td><font color="#bdbdbd">69</font></td><td><font color="#f0f0f0">16</font></td><td><font color="#a6a6a6">92</font></td><td><font color="#dfdfdf">33</font></td><td><font color="#d1d1d1">48</font></td><td><font color="#c4c4c4">61</font></td><td><font color="#d6d6d6">43</font></td><td><font color="#cdcdcd">52</font></td><td><font color="#ffffff">01</font></td><td><font color="#a9a9a9">89</font></td><td><font color="#f0f0f0">19</font></td><td><font color="#d4d4d4">67</font></td><td><font color="#e8e8e8">48</font></td></tr></table>
|
241
C/11-a.c
Executable file
241
C/11-a.c
Executable file
|
@ -0,0 +1,241 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// In the 2020 grid below, four numbers along a diagonal line have been marked in red.
|
||||
|
||||
// 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
|
||||
// The product of these numbers is 26 63 78 14 = 1788696.
|
||||
|
||||
// What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid?
|
||||
|
||||
int getValue(int);
|
||||
|
||||
int main() {
|
||||
int i, k;
|
||||
int greatestProduct = 0;
|
||||
int greatestValues[4] = {0};
|
||||
int greatestStart = 0;
|
||||
int greatestDirection = 0;
|
||||
|
||||
for(i = 0; i < 400; i++) {
|
||||
//row and column conversion
|
||||
int row = (int)(i / 20 + 0.5);
|
||||
int col = i % 20;
|
||||
|
||||
// check left/right straight line
|
||||
if(col >= 3 && col <= 16) {
|
||||
int values[4] = {
|
||||
getValue(i),
|
||||
getValue(i - 1),
|
||||
getValue(i - 2),
|
||||
getValue(i - 3),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 4; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
memcpy(greatestValues, values, 4);
|
||||
greatestStart = i;
|
||||
greatestDirection = 1;
|
||||
}
|
||||
}
|
||||
|
||||
//check up straight line
|
||||
if(row >= 3) {
|
||||
int values[4] = {
|
||||
getValue(i),
|
||||
getValue(i - 20 * 1),
|
||||
getValue(i - 20 * 2),
|
||||
getValue(i - 20 * 3),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 4; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
memcpy(greatestValues, values, 4);
|
||||
greatestStart = i;
|
||||
greatestDirection = 2;
|
||||
}
|
||||
}
|
||||
|
||||
//check down straight line
|
||||
if(row <= 16) {
|
||||
int values[4] = {
|
||||
getValue(i),
|
||||
getValue(i + 20 * 1),
|
||||
getValue(i + 20 * 2),
|
||||
getValue(i + 20 * 3),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 4; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
memcpy(greatestValues, values, 4);
|
||||
greatestStart = i;
|
||||
greatestDirection = 3;
|
||||
}
|
||||
}
|
||||
|
||||
// then check up/left diagnal
|
||||
if(row >= 3 && col >= 3) {
|
||||
int values[4] = {
|
||||
getValue(i),
|
||||
getValue(i - 21 * 1),
|
||||
getValue(i - 21 * 2),
|
||||
getValue(i - 21 * 3),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 4; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
memcpy(greatestValues, values, 4);
|
||||
greatestStart = i;
|
||||
greatestDirection = 4;
|
||||
}
|
||||
}
|
||||
|
||||
// then check down/right diagnal
|
||||
if(row <= 16 && col <= 16) {
|
||||
int values[4] = {
|
||||
getValue(i),
|
||||
getValue(i + 21 * 1),
|
||||
getValue(i + 21 * 2),
|
||||
getValue(i + 21 * 3),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 4; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
memcpy(greatestValues, values, 4);
|
||||
greatestStart = i;
|
||||
greatestDirection = 5;
|
||||
}
|
||||
}
|
||||
|
||||
// then check up/right diagnal
|
||||
if(row >= 3 && col <= 16) {
|
||||
int values[4] = {
|
||||
getValue(i),
|
||||
getValue(i - 19 * 1),
|
||||
getValue(i - 19 * 2),
|
||||
getValue(i - 19 * 3),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 4; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
memcpy(greatestValues, values, 4);
|
||||
greatestStart = i;
|
||||
greatestDirection = 6;
|
||||
}
|
||||
}
|
||||
|
||||
// then check down/left diagnal
|
||||
if(row <= 16 && col >= 3) {
|
||||
int values[4] = {
|
||||
getValue(i),
|
||||
getValue(i + 19 * 1),
|
||||
getValue(i + 19 * 2),
|
||||
getValue(i + 19 * 3),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 4; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
memcpy(greatestValues, values, 4);
|
||||
greatestStart = i;
|
||||
greatestDirection = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d\n", greatestProduct);
|
||||
printf("Starting at: %d @ ", greatestStart);
|
||||
int startRow = (int)(greatestStart / 20 + 0.5);
|
||||
int startCol = greatestStart % 20;
|
||||
printf("%d, %d going in direction %d\n", startRow, startCol, greatestDirection);
|
||||
printf("getValue(%d): %d\n", greatestStart, getValue(greatestStart));
|
||||
printf("Values: ");
|
||||
for(i = 0; i < 4; i++) {
|
||||
printf("%d, ", greatestValues[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getValue(int number) {
|
||||
static int numbersTable[20][20] = {
|
||||
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
|
||||
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
|
||||
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
|
||||
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 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, 3, 45, 2, 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, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
|
||||
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
|
||||
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
|
||||
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
|
||||
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
|
||||
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
|
||||
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
|
||||
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
|
||||
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
|
||||
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
|
||||
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
|
||||
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
|
||||
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48},
|
||||
};
|
||||
|
||||
int row = (int)(number / 20 + 0.5);
|
||||
int col = number % 20;
|
||||
|
||||
return numbersTable[row][col];
|
||||
}
|
97
C/11-b.c
Executable file
97
C/11-b.c
Executable file
|
@ -0,0 +1,97 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int getValue(int);
|
||||
|
||||
int main() {
|
||||
int i, j, current, greatestI, greatestDir;
|
||||
int greatest = 0;
|
||||
|
||||
for(i = 0; i < 400; i++) {
|
||||
//check left/right straight line
|
||||
for(j = i - 3; j <= i; j++) {
|
||||
if(j < 0) continue;
|
||||
if(i + 3 >= 400) break;
|
||||
|
||||
current = getValue(i) * getValue(i + 1) * getValue(i + 2) * getValue(i + 3);
|
||||
|
||||
if(current > greatest) {
|
||||
greatest = current;
|
||||
greatestI = i;
|
||||
greatestDir = 1;
|
||||
}
|
||||
}
|
||||
|
||||
//check up/down straight line
|
||||
for(j = i - (20 * 3); j <= i; j += 20) {
|
||||
if(j < 0) continue;
|
||||
if(i + 20 * 3 >= 400) break;
|
||||
|
||||
current = getValue(i) * getValue(i + 20 * 1) * getValue(i + 20 * 2) * getValue(i + 20 * 3);
|
||||
if(current > greatest) {
|
||||
greatest = current;
|
||||
greatestI = i;
|
||||
greatestDir = 2;
|
||||
}
|
||||
}
|
||||
|
||||
//check backslash
|
||||
for(j = i - (21 * 3); j <= i; j += 21) {
|
||||
if(j < 0) continue;
|
||||
if(i + 21 * 3 >= 400) break;
|
||||
|
||||
current = getValue(i) * getValue(i + 21 * 1) * getValue(i + 21 * 2) * getValue(i + 21 * 3);
|
||||
if(current > greatest) {
|
||||
greatest = current;
|
||||
greatestI = i;
|
||||
greatestDir = 3;
|
||||
}
|
||||
}
|
||||
|
||||
// check /
|
||||
for(j = i - (19 * 3); j <= i; j += 19) {
|
||||
if(j < 0) continue;
|
||||
if(i + 19 * 3 >= 400) break;
|
||||
|
||||
current = getValue(i) * getValue(i + 19 * 1) * getValue(i + 19 * 2) * getValue(i + 19 * 3);
|
||||
if(current > greatest) {
|
||||
greatest = current;
|
||||
greatestI = i;
|
||||
greatestDir = 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Greatest Product: %d @ %d going %d", greatest, greatestI, greatestDir);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int getValue(int number) {
|
||||
static int numbersTable[20][20] = {
|
||||
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
|
||||
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
|
||||
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
|
||||
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 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, 3, 45, 2, 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, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
|
||||
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
|
||||
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
|
||||
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
|
||||
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
|
||||
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
|
||||
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
|
||||
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
|
||||
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
|
||||
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
|
||||
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
|
||||
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
|
||||
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48},
|
||||
};
|
||||
|
||||
int row = (int)(number / 20);
|
||||
int col = number % 20;
|
||||
|
||||
return numbersTable[row][col];
|
||||
}
|
65
C/12-a.c
Executable file
65
C/12-a.c
Executable file
|
@ -0,0 +1,65 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
|
||||
|
||||
// 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||
|
||||
// Let us list the factors of the first seven triangle numbers:
|
||||
|
||||
// 1: 1
|
||||
// 3: 1,3
|
||||
// 6: 1,2,3,6
|
||||
// 10: 1,2,5,10
|
||||
// 15: 1,3,5,15
|
||||
// 21: 1,3,7,21
|
||||
// 28: 1,2,4,7,14,28
|
||||
// We can see that 28 is the first triangle number to have over five divisors.
|
||||
|
||||
// What is the value of the first triangle number to have over five hundred divisors?
|
||||
//first attempt 29 minutes, 40 seconds
|
||||
//second attempt: 13 minutes, 32 seconds
|
||||
|
||||
int countDivisors(long long);
|
||||
|
||||
int main() {
|
||||
int i = 1;
|
||||
int j;
|
||||
long long triNumber;
|
||||
|
||||
while(1) {
|
||||
if(i % 500 == 0) {
|
||||
printf("On iteration %d: \n", i);
|
||||
}
|
||||
//find the current tri number
|
||||
triNumber = ((i + 1) * i) / 2;
|
||||
i++;
|
||||
if(triNumber % 2 == 1) continue;
|
||||
|
||||
int divisors = countDivisors(triNumber);
|
||||
if(divisors > 200) {
|
||||
printf("%lld (%d divisors)\n", triNumber, divisors);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(divisors > 500) break;
|
||||
if(i < 0) break;
|
||||
}
|
||||
|
||||
printf("\n%lld", triNumber);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int countDivisors(long long number) {
|
||||
int i;
|
||||
int divisors = 0;
|
||||
|
||||
for(i = 1; i * 2 <= number; i++) {
|
||||
if(number % i == 0) {
|
||||
divisors++;
|
||||
}
|
||||
}
|
||||
|
||||
return divisors + 1;
|
||||
}
|
89
C/12-b.c
Executable file
89
C/12-b.c
Executable file
|
@ -0,0 +1,89 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
|
||||
|
||||
// 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||
|
||||
// Let us list the factors of the first seven triangle numbers:
|
||||
|
||||
// 1: 1
|
||||
// 3: 1,3
|
||||
// 6: 1,2,3,6
|
||||
// 10: 1,2,5,10
|
||||
// 15: 1,3,5,15
|
||||
// 21: 1,3,7,21
|
||||
// 28: 1,2,4,7,14,28
|
||||
// We can see that 28 is the first triangle number to have over five divisors.
|
||||
|
||||
// What is the value of the first triangle number to have over five hundred divisors?
|
||||
//first attempt 29 minutes, 40 seconds
|
||||
//second attempt: 13 minutes, 32 seconds
|
||||
//third attempt:
|
||||
|
||||
#define MAX_SAVED 100000
|
||||
|
||||
int countDivisors(long long);
|
||||
|
||||
int main() {
|
||||
int i = 1;
|
||||
int j;
|
||||
long long triNumber;
|
||||
int saved[MAX_SAVED] = {0};
|
||||
|
||||
while(1) {
|
||||
if(i % 500 == 0) {
|
||||
printf("On iteration %d: \n", i);
|
||||
}
|
||||
//find the current tri number
|
||||
triNumber = ((i + 1) * i) / 2;
|
||||
printf("[[[[[[[[[[[[[[[[[[[[[[ %lld ]]]]]]]]]]]]]]]]]]]\n", triNumber);
|
||||
if(i == 100) break;
|
||||
i++;
|
||||
if(triNumber % 2 == 1) continue;
|
||||
|
||||
int divisors = 0;
|
||||
int k = 0;
|
||||
|
||||
for(j = 2; j < triNumber && j < MAX_SAVED; j++) {
|
||||
if(triNumber % j == 0 && saved[j] != 0) {
|
||||
printf("Using %d with %d divisorsn;\n ", j, saved[j]);
|
||||
divisors = saved[j];
|
||||
k = j;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Starting iterations at %d\n", 1 + k);
|
||||
|
||||
if(j > 0) {
|
||||
for(j = 1 + k; j * 2 <= triNumber; j++) {
|
||||
if(triNumber % j == 0) {
|
||||
divisors++;
|
||||
printf("Adding divisor %d; now at %d divisors\n", j, divisors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Divisors Before: %d\n", divisors);
|
||||
divisors++;
|
||||
printf("Divisors After: %d\n", divisors);
|
||||
if(triNumber < MAX_SAVED) {
|
||||
printf("Saving with %d divisors\n", divisors);
|
||||
saved[(int)triNumber] = divisors;
|
||||
}
|
||||
|
||||
|
||||
// if(divisors > 200) {
|
||||
printf("(%d divisors)\n", divisors);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
if(divisors > 500) break;
|
||||
if(i < 0) break;
|
||||
if(i == 9) break;
|
||||
}
|
||||
|
||||
printf("\n%lld", triNumber);
|
||||
|
||||
return 1;
|
||||
}
|
69
C/12-c.c
Executable file
69
C/12-c.c
Executable file
|
@ -0,0 +1,69 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
|
||||
|
||||
// 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||
|
||||
// Let us list the factors of the first seven triangle numbers:
|
||||
|
||||
// 1: 1
|
||||
// 3: 1,3
|
||||
// 6: 1,2,3,6
|
||||
// 10: 1,2,5,10
|
||||
// 15: 1,3,5,15
|
||||
// 21: 1,3,7,21
|
||||
// 28: 1,2,4,7,14,28
|
||||
// We can see that 28 is the first triangle number to have over five divisors.
|
||||
|
||||
// What is the value of the first triangle number to have over five hundred divisors?
|
||||
//first attempt 29 minutes, 40 seconds
|
||||
//second attempt: 13 minutes, 32 seconds
|
||||
|
||||
int countDivisors(long long);
|
||||
|
||||
int main() {
|
||||
int i = 1;
|
||||
int j;
|
||||
long long triNumber;
|
||||
|
||||
while(1) {
|
||||
if(i % 500 == 0) {
|
||||
printf("On iteration %d: \n", i);
|
||||
}
|
||||
//find the current tri number
|
||||
triNumber = ((i + 1) * i) / 2;
|
||||
i++;
|
||||
if(triNumber % 2 == 1) continue;
|
||||
|
||||
int divisors = countDivisors(triNumber);
|
||||
if(divisors > 200) {
|
||||
printf("%lld (%d divisors)\n", triNumber, divisors);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(divisors > 500) break;
|
||||
if(i < 0) break;
|
||||
}
|
||||
|
||||
printf("\n%lld", triNumber);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int countDivisors(long long number) {
|
||||
int i;
|
||||
int divisors = 0;
|
||||
|
||||
for(i = 1; i * i <= number; i++) {
|
||||
if(number % i == 0) {
|
||||
divisors += 2;
|
||||
}
|
||||
}
|
||||
|
||||
if(i * i == number) {
|
||||
divisors--;
|
||||
}
|
||||
|
||||
return divisors;
|
||||
}
|
134
C/13-a.c
Executable file
134
C/13-a.c
Executable file
|
@ -0,0 +1,134 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void addBig(int currentPlace, int previousSum, char grid[][50]);
|
||||
|
||||
int main() {
|
||||
char grid[100][50] = {
|
||||
{"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"},
|
||||
};
|
||||
|
||||
addBig(49, 0, grid);
|
||||
|
||||
return 1;
|
||||
}
|
||||
void addBig(int currentPlace, int previousSum, char grid[][50]) {
|
||||
int i, currentSum = 0;
|
||||
|
||||
for(i = 0; i < 100; i++) {
|
||||
currentSum += (int)(grid[i][currentPlace] - 48);
|
||||
}
|
||||
|
||||
currentSum += previousSum;
|
||||
|
||||
char charCurrentSum[3];
|
||||
sprintf(charCurrentSum, "%03d", currentSum);
|
||||
|
||||
int nextSum = (int)(charCurrentSum[0] - 48) * 10 + (int)(charCurrentSum[1] - 48);
|
||||
|
||||
if(currentPlace-- == 0) {
|
||||
printf("%d", currentSum);
|
||||
return;
|
||||
}
|
||||
addBig(currentPlace, nextSum, grid);
|
||||
|
||||
printf("%c", charCurrentSum[2]);
|
||||
}
|
31
C/14-a.c
Executable file
31
C/14-a.c
Executable file
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int i, scorer = 0;
|
||||
int longestScore = 0;
|
||||
|
||||
for(i = 1; i < 1000000; i++) {
|
||||
if(i % 50000 == 0) printf("ON THE %d ITERATION\n", i);
|
||||
long long number = i;
|
||||
int score = 1;
|
||||
while(number != 1) {
|
||||
if(number % 2 == 0) {
|
||||
number = number / 2;
|
||||
} else {
|
||||
number = 3 * number + 1;
|
||||
}
|
||||
|
||||
score++;
|
||||
}
|
||||
|
||||
if(score > longestScore) {
|
||||
if(score > 350) printf("%d set a new high score of %d\n", i, score);
|
||||
longestScore = score;
|
||||
scorer = i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\n%d scored %d", scorer, longestScore);
|
||||
|
||||
return 1;
|
||||
}
|
33
C/15-a.c
Executable file
33
C/15-a.c
Executable file
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.
|
||||
|
||||
|
||||
// How many routes are there through a 2020 grid?
|
||||
#define GRID_SIZE 2
|
||||
|
||||
long long traverse(int x, int y);
|
||||
|
||||
int main() {
|
||||
printf("%lld", traverse(0, 0));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
long long traverse(int x, int y) {
|
||||
long long i = 0;
|
||||
printf("(%d, %d)\n", x, y);
|
||||
|
||||
if(x < GRID_SIZE) {
|
||||
i += traverse(x + 1, y);
|
||||
}
|
||||
if(y < GRID_SIZE) {
|
||||
i += traverse(x, y + 1);
|
||||
}
|
||||
|
||||
if(x == GRID_SIZE && y == GRID_SIZE) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
43
C/15-b.c
Executable file
43
C/15-b.c
Executable file
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.
|
||||
|
||||
|
||||
// How many routes are there through a 2020 grid?
|
||||
#define GRID_SIZE 20
|
||||
|
||||
long long traverse(int x, int y);
|
||||
int isCorner(int x, int y);
|
||||
|
||||
int main() {
|
||||
long long paths = 0;
|
||||
paths = traverse(0, 0);
|
||||
|
||||
printf("%lld", paths);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
long long traverse(int x, int y) {
|
||||
static long long grid[GRID_SIZE][GRID_SIZE] = {{0}};
|
||||
long long paths = 0;
|
||||
|
||||
if(GRID_SIZE - x == 0 || GRID_SIZE - y == 0) {
|
||||
grid[x][y] = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(grid[x][y] == 0) {
|
||||
if(x < GRID_SIZE) {
|
||||
paths += traverse(x + 1, y);
|
||||
}
|
||||
if(y < GRID_SIZE) {
|
||||
paths += traverse(x, y + 1);
|
||||
}
|
||||
|
||||
grid[x][y] = paths;
|
||||
grid[y][x] = paths;
|
||||
}
|
||||
|
||||
return grid[x][y];
|
||||
}
|
40
C/15-giudoku.c
Executable file
40
C/15-giudoku.c
Executable file
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define LEN 21
|
||||
|
||||
void printMatrice(unsigned long long m[][LEN])
|
||||
{
|
||||
int i,j;
|
||||
|
||||
for (i = 0; i < LEN; ++i)
|
||||
{
|
||||
for (j = 0; j < LEN; ++j)
|
||||
{
|
||||
printf ("%11llu ", m[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned long long matrice[LEN][LEN] = {0};
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < LEN; ++i)
|
||||
{
|
||||
matrice[i][0] = matrice[0][i] = 1;
|
||||
}
|
||||
matrice[0][0] = 0;
|
||||
|
||||
for (i = 1; i < LEN; ++i)
|
||||
{
|
||||
for (j = 1; j < LEN; ++j)
|
||||
{
|
||||
matrice[i][j] = matrice[i-1][j] + matrice[i][j-1];
|
||||
}
|
||||
}
|
||||
|
||||
printMatrice(matrice);
|
||||
return 0;
|
||||
}
|
41
C/16-a.c
Executable file
41
C/16-a.c
Executable file
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
|
||||
|
||||
// What is the sum of the digits of the number 2^1000?
|
||||
|
||||
#define POWER 1000
|
||||
#define MAXSIZE 400
|
||||
|
||||
void power(int base, int power, int* digits);
|
||||
|
||||
int main() {
|
||||
int digits[MAXSIZE] = {0};
|
||||
int i, sum = 0;
|
||||
|
||||
digits[0] = 1;
|
||||
|
||||
power(2, POWER, digits);
|
||||
|
||||
for(i = 0; i < MAXSIZE; i++) {
|
||||
sum += digits[i];
|
||||
}
|
||||
|
||||
printf("%d\n", sum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void power(int base, int power, int* digits) {
|
||||
int i, j;
|
||||
|
||||
for(i = 1; i <= power; i++) {
|
||||
int current = 1, carry = 0;
|
||||
|
||||
for(j = 0; j < MAXSIZE; j++) {
|
||||
current = (digits[j] << 1) + carry;
|
||||
digits[j] = current % 10;
|
||||
carry = current / 10;
|
||||
}
|
||||
}
|
||||
}
|
31
C/17-a.c
Executable file
31
C/17-a.c
Executable file
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
|
||||
|
||||
// If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
|
||||
|
||||
|
||||
// NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
|
||||
|
||||
int main(void) {
|
||||
int ones[] = {0, 3, 3, 5, 4, 4, 3, 5, 5, 4};
|
||||
int teens[] = {3, 6, 6, 8, 8, 7, 7, 9, 8, 8};
|
||||
int tens[] = {0, 0, 6, 6, 5, 5, 5, 7, 6, 6};
|
||||
|
||||
int i, onesSum = 0, teensSum= 0, tensSum= 0, hundredsSum = 0;
|
||||
|
||||
for(i = 0; i < 10; i++)
|
||||
onesSum += ones[i];
|
||||
for(i = 0; i < 10; i++)
|
||||
teensSum += teens[i];
|
||||
for(i = 0; i < 10; i++)
|
||||
tensSum += tens[i];
|
||||
|
||||
int length = 0;
|
||||
// ( ones teens tens ) ten times (XX hundred) ("hundred") ("and") ("thousand")
|
||||
length = ((onesSum * 9 + teensSum + tensSum * 10) * 10) + (onesSum * 100) + (7*100*9) + (3*99*9) + 11;
|
||||
|
||||
printf("%d\n", length);
|
||||
|
||||
return 1;
|
||||
}
|
77
C/18-a.c
Executable file
77
C/18-a.c
Executable file
|
@ -0,0 +1,77 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
|
||||
|
||||
// 3
|
||||
// 7 4
|
||||
// 2 4 6
|
||||
// 8 5 9 3
|
||||
|
||||
// That is, 3 + 7 + 4 + 9 = 23.
|
||||
|
||||
// Find the maximum total from top to bottom of the triangle below:
|
||||
|
||||
// 75 0 0
|
||||
// 95 64 2 2
|
||||
// 17 47 82 5 3
|
||||
// 18 35 87 10 9 4
|
||||
// 20 04 82 47 65 14 5
|
||||
// 19 01 23 75 03 34 20 6
|
||||
// 88 02 77 73 07 63 67 27 7
|
||||
// 99 65 04 28 06 16 70 92
|
||||
// 41 41 26 56 83 40 80 70 33
|
||||
// 41 48 72 33 47 32 37 16 94 29
|
||||
// 53 71 44 65 25 43 91 52 97 51 14
|
||||
// 70 11 33 28 77 73 17 78 39 68 17 57
|
||||
// 91 71 52 38 17 14 91 43 58 50 27 29 48
|
||||
// 63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||
// 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
||||
|
||||
// NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
|
||||
|
||||
#define SIZE 15
|
||||
|
||||
int traverseTriangle(int x, int y, int size);
|
||||
int triangleSize(int triangle[]);
|
||||
|
||||
int main(void) {
|
||||
// int triangle[4][4] = {
|
||||
// {3, 0, 0, 0},
|
||||
// {7, 4, 0, 0},
|
||||
// {2, 4, 7, 0},
|
||||
// {8, 5, 9, 3}
|
||||
// };
|
||||
int triangle[15][15] = {
|
||||
{75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{95, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{17, 47, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{18, 35, 87, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{20, 4, 82, 47, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{19, 1, 23, 75, 3, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{88, 2, 77, 73, 7, 63, 67, 0, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{99, 65, 4, 28, 6, 16, 70, 92, 0, 0, 0, 0, 0, 0, 0, },
|
||||
{41, 41, 26, 56, 83, 40, 80, 70, 33, 0, 0, 0, 0, 0, 0, },
|
||||
{41, 48, 72, 33, 47, 32, 37, 17, 94, 29, 0, 0, 0, 0, 0, },
|
||||
{53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14, 0, 0, 0, 0, },
|
||||
{70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57, 0, 0, 0, },
|
||||
{91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48, 0, 0, },
|
||||
{63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31, 0, },
|
||||
{ 4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23, },
|
||||
};
|
||||
int i, j;
|
||||
|
||||
// size = 10; how to find last row
|
||||
for(i = SIZE - 1; i >= 0; i--) {
|
||||
for(j = 0; j <= SIZE - 1; j++) {
|
||||
if(triangle[i][j] + triangle[i - 1][j] > triangle[i][j + 1] + triangle [i - 1][j]) {
|
||||
triangle[i - 1][j] = triangle[i - 1][j] + triangle[i][j];
|
||||
} else {
|
||||
triangle[i - 1][j] = triangle[i - 1][j] + triangle[i][j + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d", triangle[0][0]);
|
||||
|
||||
return 1;
|
||||
}
|
54
C/19-a.c
Executable file
54
C/19-a.c
Executable file
|
@ -0,0 +1,54 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// You are given the following information, but you may prefer to do some research for yourself.
|
||||
|
||||
// 1 Jan 1900 was a Monday.
|
||||
// Thirty days has September,
|
||||
// April, June and November.
|
||||
// All the rest have thirty-one,
|
||||
// Saving February alone,
|
||||
// Which has twenty-eight, rain or shine.
|
||||
// And on leap years, twenty-nine.
|
||||
// A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
|
||||
// How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
||||
|
||||
int main(void) {
|
||||
int year = 1900, month = 1, day = 1, firstSunday = 0;
|
||||
|
||||
for(; year <= 2000; year++) {
|
||||
printf("%d: ", year);
|
||||
for(month = 1; month <= 12; month++) {
|
||||
int daysInMonth;
|
||||
if(month == 4 || month == 6 || month == 9 || month == 11) {
|
||||
daysInMonth = 30;
|
||||
} else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
|
||||
daysInMonth = 31;
|
||||
} else if(year % 100 == 0) {
|
||||
if(year % 400 == 0) {
|
||||
daysInMonth = 29;
|
||||
} else {
|
||||
daysInMonth = 28;
|
||||
}
|
||||
} else if(year % 4 == 0) {
|
||||
daysInMonth = 29;
|
||||
} else {
|
||||
daysInMonth = 28;
|
||||
}
|
||||
|
||||
if(day % 7 == 0) {
|
||||
printf("X");
|
||||
if(year != 1900)
|
||||
firstSunday++;
|
||||
} else {
|
||||
printf("O");
|
||||
}
|
||||
|
||||
day += daysInMonth;
|
||||
}
|
||||
printf("\t%d\n", firstSunday);
|
||||
}
|
||||
|
||||
printf("%d\n", firstSunday);
|
||||
|
||||
return 1;
|
||||
}
|
25
C/19-b.c
Executable file
25
C/19-b.c
Executable file
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
int year, month, day = 1, firstSunday = 0;
|
||||
|
||||
for(year = 1900; year <= 2000; year++) {
|
||||
for(month = 0; month < 12; month++) {
|
||||
int daysInMonth = months[month];
|
||||
|
||||
if(month == 1 && year % 100 == 0) {
|
||||
if(year % 400 == 0)
|
||||
daysInMonth++;
|
||||
} else if(month == 1 && year % 4 == 0)
|
||||
daysInMonth++;
|
||||
|
||||
if(day % 7 == 0 && year != 1900)
|
||||
firstSunday++;
|
||||
|
||||
day += daysInMonth;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d", firstSunday);
|
||||
}
|
25
C/2-a.c
Executable file
25
C/2-a.c
Executable file
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
|
||||
|
||||
// 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
|
||||
|
||||
// By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
||||
|
||||
void main() {
|
||||
int i = 1;
|
||||
int j,sum = 0;
|
||||
|
||||
while(i < 4000000) {
|
||||
printf("%d, ", i);
|
||||
if(i % 2 == 0) {
|
||||
sum += i;
|
||||
}
|
||||
|
||||
int temp = i;
|
||||
i = i + j;
|
||||
j = temp;
|
||||
}
|
||||
|
||||
printf("\n\nSum: %d", sum);
|
||||
}
|
34
C/20-a.c
Executable file
34
C/20-a.c
Executable file
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// n! means n (n 1) ... 3 2 1
|
||||
|
||||
// For example, 10! = 10 9 ... 3 2 1 = 3628800,
|
||||
// and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
|
||||
|
||||
// Find the sum of the digits in the number 100!
|
||||
|
||||
#define NUMBER 100
|
||||
#define MAXSIZE 500
|
||||
|
||||
int main(void) {
|
||||
int factorial [MAXSIZE] = {0};
|
||||
int i, j, sum = 0;
|
||||
|
||||
factorial[0] = 1;
|
||||
for(i = NUMBER; i > 0; i--) {
|
||||
int current = 1, carry = 0;
|
||||
|
||||
for(j = 0; j < MAXSIZE; j++) {
|
||||
current = factorial[j] * i + carry;
|
||||
factorial[j] = current % 10;
|
||||
carry = current / 10;
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < MAXSIZE; i++)
|
||||
sum += factorial[i];
|
||||
|
||||
printf("%d", sum);
|
||||
|
||||
return 1;
|
||||
}
|
43
C/21-a.c
Executable file
43
C/21-a.c
Executable file
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// 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.
|
||||
|
||||
#define UPPER 10000
|
||||
|
||||
int sumDivisors(int number);
|
||||
|
||||
int main(void) {
|
||||
int divisors[UPPER] = {0};
|
||||
int i, amicableSum = 0;
|
||||
|
||||
for(i = 0; i < UPPER; i++)
|
||||
divisors[i] = sumDivisors(i);
|
||||
|
||||
for(i = 0; i < UPPER; i++) {
|
||||
if(divisors[i] < UPPER) {
|
||||
if(divisors[divisors[i]] == i && divisors[i] != i) {
|
||||
printf("%d and %d are amicable\n", i, divisors[i]);
|
||||
amicableSum += i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d", amicableSum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sumDivisors(int number) {
|
||||
int i, sum = 0;
|
||||
|
||||
for(i = 1; i * 2 <= number; i++)
|
||||
if(number % i == 0)
|
||||
sum += i;
|
||||
|
||||
return sum;
|
||||
}
|
62
C/22-a.c
Executable file
62
C/22-a.c
Executable file
|
@ -0,0 +1,62 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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?
|
||||
|
||||
int nameScore(char name[50]);
|
||||
|
||||
int main(void) {
|
||||
char c;
|
||||
char names[5163][50];
|
||||
int i, name = 0, character = 0, inQuotes = 0;
|
||||
long long totalScore = 0;
|
||||
|
||||
while((c = getchar()) != EOF) {
|
||||
if(c == '"') {
|
||||
if(inQuotes == 0) {
|
||||
inQuotes = 1;
|
||||
} else {
|
||||
inQuotes = 0;
|
||||
names[name][character] = '\0';
|
||||
name++;
|
||||
character = 0;
|
||||
}
|
||||
} else if(c != ',') {
|
||||
names[name][character] = c;
|
||||
character++;
|
||||
}
|
||||
}
|
||||
|
||||
qsort(names, 5163, 50, (int(*)(const void*, const void*))strcmp);
|
||||
|
||||
for(i = 0; i < name; i++) {
|
||||
int scoredName = nameScore(names[i]);
|
||||
int currentScore = (i + 1) * scoredName;
|
||||
totalScore += currentScore;
|
||||
}
|
||||
|
||||
printf("%lld", totalScore);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int nameScore(char name[50]) {
|
||||
char alphabet[27] = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
int i, j, nameScore = 0;
|
||||
|
||||
for(i = 0; name[i] != '\0'; i++) {
|
||||
for(j = 1; j < 27; j++) {
|
||||
if(name[i] == alphabet[j]) {
|
||||
nameScore += j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nameScore;
|
||||
}
|
66
C/22-b.c
Executable file
66
C/22-b.c
Executable file
|
@ -0,0 +1,66 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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?
|
||||
|
||||
int nameScore(char name[50]);
|
||||
|
||||
int main(void) {
|
||||
char c;
|
||||
char (*names)[50] = malloc(10 * sizeof(char[50]));
|
||||
int i, name = 0, character = 0, inQuotes = 0, capacity = 10;
|
||||
long long totalScore = 0;
|
||||
|
||||
while((c = getchar()) != EOF) {
|
||||
if(name == capacity) {
|
||||
capacity *= 2;
|
||||
names = realloc(names, capacity * sizeof(char[50]));
|
||||
}
|
||||
if(c == '"') {
|
||||
if(inQuotes == 0) {
|
||||
inQuotes = 1;
|
||||
} else {
|
||||
inQuotes = 0;
|
||||
names[name][character] = '\0';
|
||||
name++;
|
||||
character = 0;
|
||||
}
|
||||
} else if(c != ',') {
|
||||
names[name][character] = c;
|
||||
character++;
|
||||
}
|
||||
}
|
||||
|
||||
qsort(names, name, sizeof(char[50]), (int(*)(const void*, const void*))strcmp);
|
||||
|
||||
for(i = 0; i < name; i++) {
|
||||
int scoredName = nameScore(names[i]);
|
||||
int currentScore = (i + 1) * scoredName;
|
||||
totalScore += currentScore;
|
||||
}
|
||||
|
||||
printf("%lld", totalScore);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int nameScore(char name[50]) {
|
||||
char alphabet[] = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
int i, j, nameScore = 0;
|
||||
|
||||
for(i = 0; name[i] != '\0'; i++) {
|
||||
for(j = 1; alphabet[j] != '\0'; j++) {
|
||||
if(name[i] == alphabet[j]) {
|
||||
nameScore += j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nameScore;
|
||||
}
|
1
C/22-small.txt
Executable file
1
C/22-small.txt
Executable file
|
@ -0,0 +1 @@
|
|||
"MARY","PATRICIA"
|
1
C/22.txt
Executable file
1
C/22.txt
Executable file
File diff suppressed because one or more lines are too long
57
C/23-a.c
Executable file
57
C/23-a.c
Executable file
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// 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.
|
||||
|
||||
#define DEFICITMAX 28123
|
||||
int divisorSum(int number);
|
||||
|
||||
int main(void) {
|
||||
int abundantSums[DEFICITMAX] = {1};
|
||||
int abundantAdds[DEFICITMAX] = {0};
|
||||
int i, j, abundantAddSum = 0;
|
||||
|
||||
for(i = 1; i <= DEFICITMAX; i++) {
|
||||
abundantSums[i] = divisorSum(i);
|
||||
|
||||
if(abundantSums[i] <= i)
|
||||
abundantSums[i] = 0;
|
||||
}
|
||||
|
||||
for(i = 1; i <= DEFICITMAX; i++) {
|
||||
if(abundantSums[i] > 0) {
|
||||
for(j = 1; i + j <= DEFICITMAX; j++) {
|
||||
if(abundantSums[j] > 0 && abundantAdds[i + j] != 1) {
|
||||
abundantAdds[i + j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 1; i <= DEFICITMAX; i++) {
|
||||
if(abundantAdds[i] == 0) {
|
||||
abundantAddSum += i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\n = %d", abundantAddSum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int divisorSum(int number) {
|
||||
int i, sum = 0;
|
||||
|
||||
for(i = 1; i <= number / 2; i++)
|
||||
if(number % i == 0) {
|
||||
sum += i;
|
||||
}
|
||||
|
||||
|
||||
return sum;
|
||||
}
|
48
C/23-b.c
Executable file
48
C/23-b.c
Executable file
|
@ -0,0 +1,48 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define DEFICITMAX 28123
|
||||
|
||||
int divisorSum(int number);
|
||||
|
||||
int main(void) {
|
||||
int abundantNumbers[DEFICITMAX] = {0};
|
||||
int sumOfAbundantNumbers[DEFICITMAX] = {0};
|
||||
int i, j, sum = 0;
|
||||
|
||||
// generate list of abundant numbers 1-DEFICITMAX
|
||||
for(i = 1; i <= DEFICITMAX; i++)
|
||||
if(isAbundant(i) == 1)
|
||||
abundantNumbers[i] = 1;
|
||||
|
||||
// generate list of all numbers that are the sum of abundant numbers
|
||||
for(i = 1; i <= DEFICITMAX; i++) {
|
||||
if(abundantNumbers[i] > 0) {
|
||||
for(j = 1; i + j <= DEFICITMAX; j++) {
|
||||
if(abundantNumbers[j] > 0 && sumOfAbundantNumbers[i + j] != 1) {
|
||||
sumOfAbundantNumbers[i + j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sum up all positive integers that cannot be written as the sum
|
||||
for(i = 1; i <= DEFICITMAX; i++) {
|
||||
if(sumOfAbundantNumbers[i] == 0) {
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\n%d", sum);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int isAbundant(int number) {
|
||||
int i, sum = 0;
|
||||
|
||||
for(i = 1; i <= number / 2; i++)
|
||||
if(number % i == 0)
|
||||
sum += i;
|
||||
|
||||
return (sum > number) ? 1 : 0;
|
||||
}
|
59
C/24-a.c
Executable file
59
C/24-a.c
Executable file
|
@ -0,0 +1,59 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
|
||||
|
||||
// 012 021 102 120 201 210
|
||||
|
||||
// What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||
|
||||
#define MAXPERM 3
|
||||
|
||||
void swap(int i, int j, int numbers[]);
|
||||
void permutate(int numbers[]);
|
||||
|
||||
int main(void) {
|
||||
// int numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
int numbers[] = { 0, 1, 2 };
|
||||
int i, permutations = 0;
|
||||
|
||||
i = sizeof(numbers) - 1;
|
||||
while(permutations < MAXPERM) {
|
||||
while(numbers[i - 1] >= numbers[i]) {
|
||||
i--;
|
||||
}
|
||||
|
||||
int j = sizeof(numbers);
|
||||
|
||||
while(numbers[j - 1] <= numbers[i - 1]) {
|
||||
j = j - 1;
|
||||
}
|
||||
|
||||
swap(i - 1, j - 1, numbers);
|
||||
|
||||
i++;
|
||||
j = sizeof(numbers);
|
||||
|
||||
while(i < j) {
|
||||
swap(i - 1, j - 1, numbers);
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
|
||||
permutations++;
|
||||
}
|
||||
|
||||
for(i = 0; i < MAXPERM; i++)
|
||||
printf("%d, ", numbers[i]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void permutate(int numbers[]) {
|
||||
|
||||
}
|
||||
|
||||
void swap(int i, int j, int numbers[]) {
|
||||
int temp = numbers[i];
|
||||
numbers[i] = numbers[j];
|
||||
numbers[j] = temp;
|
||||
}
|
57
C/24-b.c
Executable file
57
C/24-b.c
Executable file
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define GETPERM 1000000
|
||||
|
||||
void permutate(int set[], int setSize);
|
||||
void swap(int a, int b, int numbers[]);
|
||||
void printSet(int set[], int setSize);
|
||||
|
||||
int main(void) {
|
||||
int numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
int setSize = sizeof(numbers) / sizeof(numbers[0]);
|
||||
|
||||
permutate(numbers, setSize);
|
||||
|
||||
printf("\nPermutation %d: ", GETPERM);
|
||||
printSet(numbers, setSize);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void permutate(int a[], int setSize) {
|
||||
int permutations = 0;
|
||||
int k, l;
|
||||
|
||||
while(permutations < GETPERM - 1) {
|
||||
// Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation
|
||||
for(k = setSize - 2; a[k] >= a[k + 1]; k--)
|
||||
;
|
||||
|
||||
|
||||
// Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index, l is well defined and satisfies k < l.
|
||||
for(l = setSize - 1; a[k] >= a[l]; l--)
|
||||
;
|
||||
|
||||
// Swap a[k] with a[l].
|
||||
swap(k, l, a);
|
||||
|
||||
// Reverse the sequence from a[k + 1] up to and including the final element a[n].
|
||||
for(k++, l = setSize - 1; k < l; k++, l--)
|
||||
swap(k, l, a);
|
||||
|
||||
permutations++;
|
||||
}
|
||||
}
|
||||
|
||||
void swap(int a, int b, int set[]) {
|
||||
int temp = set[b];
|
||||
set[b] = set[a];
|
||||
set[a] = temp;
|
||||
}
|
||||
|
||||
void printSet(int set[], int setSize) {
|
||||
int i;
|
||||
|
||||
for(i = 0; i < setSize; i++)
|
||||
printf("%d, ", set[i]);
|
||||
}
|
45
C/24-mathblog.c
Executable file
45
C/24-mathblog.c
Executable file
|
@ -0,0 +1,45 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int[] perm = { 0, 1, 2 };
|
||||
|
||||
int count = 1;
|
||||
int numPerm = 3a;
|
||||
|
||||
while (count < numPerm) {
|
||||
int N = perm.Length;
|
||||
int i = N-1;
|
||||
while (perm[i - 1] >= perm[i]) {
|
||||
i = i - 1;
|
||||
}
|
||||
|
||||
int j = N;
|
||||
while (perm[j - 1] <= perm[i - 1]) {
|
||||
j = j - 1;
|
||||
}
|
||||
|
||||
// swap values at position i-1 and j-1
|
||||
swap(i - 1, j - 1);
|
||||
|
||||
i++;
|
||||
j = N;
|
||||
while (i < j) {
|
||||
swap(i - 1, j - 1);
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
string permNum = "";
|
||||
for (int k = 0; k < perm.Length; k++) {
|
||||
permNum = permNum + perm[k];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void swap(int i, int j) {
|
||||
int k = perm[i];
|
||||
perm[i] = perm[j];
|
||||
perm[j] = k;
|
||||
}
|
36
C/25-a.c
Executable file
36
C/25-a.c
Executable file
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
// The Fibonacci sequence is defined by the recurrence relation:
|
||||
|
||||
// F(n) = F(n-1) + F(n-2), where F(1) = 1 and F(2) = 1.
|
||||
// Hence the first 12 terms will be:
|
||||
|
||||
// F(1) = 1
|
||||
// F(2) = 1
|
||||
// F(3) = 2
|
||||
// F(4) = 3
|
||||
// F(5) = 5
|
||||
// F(6) = 8
|
||||
// F(7) = 13
|
||||
// F(8) = 21
|
||||
// F(9) = 34
|
||||
// F(10) = 55
|
||||
// F(11) = 89
|
||||
// F(12) = 144
|
||||
// The 12th term, F(12), is the first term to contain three digits.
|
||||
|
||||
// What is the first term in the Fibonacci sequence to contain 1000 digits?
|
||||
|
||||
#define DIGITS 1000
|
||||
|
||||
int main(void) {
|
||||
long double goldenRatio = (1 + sqrt(5)) / 2;
|
||||
|
||||
// int number = (int)ceil((DIGITS - 1 * log10(5)) / log10(goldenRatio));
|
||||
int number = (int)ceil((DIGITS - 1 + log10(5) / 2) / log10(goldenRatio));
|
||||
|
||||
printf("%d", number);
|
||||
|
||||
return 1;
|
||||
}
|
15
C/25-test.c
Executable file
15
C/25-test.c
Executable file
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#define DIGITS 100000
|
||||
|
||||
int main(void) {
|
||||
long double goldenRatio = (1 + sqrt(5)) / 2;
|
||||
|
||||
int number1 = (int)ceil((DIGITS - 1 * log10(5)) / log10(goldenRatio));
|
||||
int number2 = (int)ceil((DIGITS - 1 + log10(5) / 2) / log10(goldenRatio));
|
||||
|
||||
printf("%d = %d\n", number1, number2);
|
||||
|
||||
return 1;
|
||||
}
|
50
C/26-a.c
Executable file
50
C/26-a.c
Executable file
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
|
||||
|
||||
// 1/2 = 0.5
|
||||
// 1/3 = 0.(3)
|
||||
// 1/4 = 0.25
|
||||
// 1/5 = 0.2
|
||||
// 1/6 = 0.1(6)
|
||||
// 1/7 = 0.(142857)
|
||||
// 1/8 = 0.125
|
||||
// 1/9 = 0.(1)
|
||||
// 1/10 = 0.1
|
||||
// Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
|
||||
|
||||
// Find the value of d 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
|
||||
|
||||
#define UPPER 1000
|
||||
|
||||
char* convertToDigits(double number);
|
||||
|
||||
int main(void) {
|
||||
int i, j = -1, primes[UPPER] = {0};
|
||||
|
||||
for(i = 1; i < UPPER; i++)
|
||||
if(isPrime(i))
|
||||
primes[j++] = i;
|
||||
|
||||
for(i = 0; i < j; i++) {
|
||||
printf("1/%d: %.15f\n", primes[i], 1.0 / primes[i]);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* convertToDigits(double number) {
|
||||
|
||||
}
|
||||
|
||||
int isPrime(int number) {
|
||||
int i;
|
||||
|
||||
for(i = 2; i * i < number; i += 2) {
|
||||
if(number % i == 0 && i != number) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
37
C/3-a.c
Executable file
37
C/3-a.c
Executable file
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
// The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
|
||||
// What is the largest prime factor of the number 600,851,475,143 ?
|
||||
void main() {
|
||||
long long number = 600851475143;
|
||||
|
||||
int i = (int) (sqrt(number) + 0.5);
|
||||
|
||||
while(i > 2) {
|
||||
double quotient = (double)number / (double)i;
|
||||
|
||||
if(quotient / 1.0 == (int)quotient) {
|
||||
int bool = isPrime(i);
|
||||
if(bool == 1) {
|
||||
printf("\n\nLargest Prime Factor: %d", i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//decrement i; preferably only going through odds?
|
||||
i = i - 1;
|
||||
}
|
||||
}
|
||||
|
||||
int isPrime(int number) {
|
||||
int i;
|
||||
for(i = 2; i < number; i++) {
|
||||
if(number % i == 0 && i != number) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
37
C/3-b.c
Executable file
37
C/3-b.c
Executable file
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
// The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
|
||||
// What is the largest prime factor of the number 600,851,475,143 ?
|
||||
void main() {
|
||||
long long number = 600851475143;
|
||||
|
||||
int i = (int) (sqrt(number) + 0.5);
|
||||
|
||||
while(i > 2) {
|
||||
double quotient = (double)number / (double)i;
|
||||
|
||||
if(quotient / 1.0 == (int)quotient) {
|
||||
int bool = isPrime(i);
|
||||
if(bool == 1) {
|
||||
printf("\n\nLargest Prime Factor: %d", i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//decrement i; preferably only going through odds?
|
||||
i = i - 1;
|
||||
}
|
||||
}
|
||||
|
||||
int isPrime(int number) {
|
||||
int i;
|
||||
for(i = 2; i < number; i++) {
|
||||
if(number % i == 0 && i != number) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
14
C/3-bitrake.c
Executable file
14
C/3-bitrake.c
Executable file
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void main() {
|
||||
long long number = 600851475143;
|
||||
int divisor = 2;
|
||||
while (number > 1) {
|
||||
if (0 == (number % divisor)) {
|
||||
number /= divisor;
|
||||
divisor--;
|
||||
}
|
||||
divisor++;
|
||||
}
|
||||
printf("%d", divisor);
|
||||
}
|
34
C/4-a.c
Executable file
34
C/4-a.c
Executable file
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
|
||||
|
||||
// Find the largest palindrome made from the product of two 3-digit numbers.
|
||||
|
||||
void main() {
|
||||
int i, j, k, palindrome;
|
||||
|
||||
for(i = 100; i < 1000; i++) {
|
||||
for(j = 100; j < 1000; j++) {
|
||||
int product = i * j;
|
||||
|
||||
//test if product is palindrome and store
|
||||
int reverse = 0, number = product;
|
||||
|
||||
while(number > 0) {
|
||||
int last = number % 10; // find the last digit
|
||||
reverse = reverse * 10 + last; // push over the reverse and tack on the last digit
|
||||
number = number / 10; // chop off the last digit
|
||||
}
|
||||
|
||||
if(product == reverse) {
|
||||
if(product > palindrome) {
|
||||
palindrome = product;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d", palindrome);
|
||||
}
|
26
C/5-a.c
Executable file
26
C/5-a.c
Executable file
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
|
||||
|
||||
// What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
|
||||
void main() {
|
||||
int i = 20; //no smaller than this
|
||||
int j;
|
||||
|
||||
while(1) {
|
||||
for(j = 19; j > 0; j--) {
|
||||
if(i % j != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(j == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
i += 20;
|
||||
}
|
||||
|
||||
printf("%d", i);
|
||||
}
|
31
C/6-a.c
Executable file
31
C/6-a.c
Executable file
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// The sum of the squares of the first ten natural numbers is,
|
||||
|
||||
// 1^2 + 2^2 + ... + 10^2 = 385
|
||||
// The square of the sum of the first ten natural numbers is,
|
||||
|
||||
// (1 + 2 + ... + 10)^2 = 55^2 = 3025
|
||||
// Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
|
||||
|
||||
// Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
||||
|
||||
void main() {
|
||||
int sumOfSquares = 0;
|
||||
|
||||
int i = 0;
|
||||
for(i = 1; i <= 100; i++) {
|
||||
sumOfSquares += i * i;
|
||||
}
|
||||
|
||||
int squareOfSums = 0;
|
||||
for(i = 1; i <= 100; i++) {
|
||||
squareOfSums += i;
|
||||
}
|
||||
|
||||
squareOfSums *= squareOfSums;
|
||||
|
||||
int difference = squareOfSums - sumOfSquares;
|
||||
|
||||
printf("%d", difference);
|
||||
}
|
36
C/7-a.c
Executable file
36
C/7-a.c
Executable file
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
|
||||
|
||||
// What is the 10 001st prime number?
|
||||
|
||||
void main() {
|
||||
int primes = 0;
|
||||
int i = 1;
|
||||
|
||||
while(1) {
|
||||
if(isPrime(i) == 1) {
|
||||
primes++;
|
||||
}
|
||||
|
||||
if(primes == 10001) {
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
printf("%d", i);
|
||||
}
|
||||
|
||||
int isPrime(int number) {
|
||||
int i;
|
||||
|
||||
if(number % 2 == 0) return 0;
|
||||
|
||||
for(i = number; i > 2; i -= 2) {
|
||||
if(number % i == 0 && i != number) return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
83
C/8-a.c
Executable file
83
C/8-a.c
Executable file
|
@ -0,0 +1,83 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// Find the greatest product of five consecutive digits in the 1000-digit number.
|
||||
|
||||
// 73167176531330624919225119674426574742355349194934
|
||||
// 96983520312774506326239578318016984801869478851843
|
||||
// 85861560789112949495459501737958331952853208805511
|
||||
// 12540698747158523863050715693290963295227443043557
|
||||
// 66896648950445244523161731856403098711121722383113
|
||||
// 62229893423380308135336276614282806444486645238749
|
||||
// 30358907296290491560440772390713810515859307960866
|
||||
// 70172427121883998797908792274921901699720888093776
|
||||
// 65727333001053367881220235421809751254540594752243
|
||||
// 52584907711670556013604839586446706324415722155397
|
||||
// 53697817977846174064955149290862569321978468622482
|
||||
// 83972241375657056057490261407972968652414535100474
|
||||
// 82166370484403199890008895243450658541227588666881
|
||||
// 16427171479924442928230863465674813919123162824586
|
||||
// 17866458359124566529476545682848912883142607690042
|
||||
// 24219022671055626321111109370544217506941658960408
|
||||
// 07198403850962455444362981230987879927244284909188
|
||||
// 84580156166097919133875499200524063689912560717606
|
||||
// 05886116467109405077541002256983155200055935729725
|
||||
// 71636269561882670428252483600823257530420752963450
|
||||
|
||||
|
||||
void main() {
|
||||
int i,k;
|
||||
int greatestProduct = 0;
|
||||
for(i = 0; i < 1000; i++) {
|
||||
if(i >= 4) {
|
||||
//get previous 5 locations
|
||||
int values[5] = {
|
||||
getValue(i),
|
||||
getValue(i - 1),
|
||||
getValue(i - 2),
|
||||
getValue(i - 3),
|
||||
getValue(i - 4),
|
||||
};
|
||||
|
||||
int currentProduct = 1;
|
||||
for(k = 0; k < 5; k++) {
|
||||
currentProduct *= values[k];
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d", greatestProduct);
|
||||
}
|
||||
|
||||
int getValue(int number) {
|
||||
int oneThousand[20][50] = {
|
||||
{7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4},
|
||||
{9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3},
|
||||
{8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1},
|
||||
{1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5,5,7},
|
||||
{6,6,8,9,6,6,4,8,9,5,0,4,4,5,2,4,4,5,2,3,1,6,1,7,3,1,8,5,6,4,0,3,0,9,8,7,1,1,1,2,1,7,2,2,3,8,3,1,1,3},
|
||||
{6,2,2,2,9,8,9,3,4,2,3,3,8,0,3,0,8,1,3,5,3,3,6,2,7,6,6,1,4,2,8,2,8,0,6,4,4,4,4,8,6,6,4,5,2,3,8,7,4,9},
|
||||
{3,0,3,5,8,9,0,7,2,9,6,2,9,0,4,9,1,5,6,0,4,4,0,7,7,2,3,9,0,7,1,3,8,1,0,5,1,5,8,5,9,3,0,7,9,6,0,8,6,6},
|
||||
{7,0,1,7,2,4,2,7,1,2,1,8,8,3,9,9,8,7,9,7,9,0,8,7,9,2,2,7,4,9,2,1,9,0,1,6,9,9,7,2,0,8,8,8,0,9,3,7,7,6},
|
||||
{6,5,7,2,7,3,3,3,0,0,1,0,5,3,3,6,7,8,8,1,2,2,0,2,3,5,4,2,1,8,0,9,7,5,1,2,5,4,5,4,0,5,9,4,7,5,2,2,4,3},
|
||||
{5,2,5,8,4,9,0,7,7,1,1,6,7,0,5,5,6,0,1,3,6,0,4,8,3,9,5,8,6,4,4,6,7,0,6,3,2,4,4,1,5,7,2,2,1,5,5,3,9,7},
|
||||
{5,3,6,9,7,8,1,7,9,7,7,8,4,6,1,7,4,0,6,4,9,5,5,1,4,9,2,9,0,8,6,2,5,6,9,3,2,1,9,7,8,4,6,8,6,2,2,4,8,2},
|
||||
{8,3,9,7,2,2,4,1,3,7,5,6,5,7,0,5,6,0,5,7,4,9,0,2,6,1,4,0,7,9,7,2,9,6,8,6,5,2,4,1,4,5,3,5,1,0,0,4,7,4},
|
||||
{8,2,1,6,6,3,7,0,4,8,4,4,0,3,1,9,9,8,9,0,0,0,8,8,9,5,2,4,3,4,5,0,6,5,8,5,4,1,2,2,7,5,8,8,6,6,6,8,8,1},
|
||||
{1,6,4,2,7,1,7,1,4,7,9,9,2,4,4,4,2,9,2,8,2,3,0,8,6,3,4,6,5,6,7,4,8,1,3,9,1,9,1,2,3,1,6,2,8,2,4,5,8,6},
|
||||
{1,7,8,6,6,4,5,8,3,5,9,1,2,4,5,6,6,5,2,9,4,7,6,5,4,5,6,8,2,8,4,8,9,1,2,8,8,3,1,4,2,6,0,7,6,9,0,0,4,2},
|
||||
{2,4,2,1,9,0,2,2,6,7,1,0,5,5,6,2,6,3,2,1,1,1,1,1,0,9,3,7,0,5,4,4,2,1,7,5,0,6,9,4,1,6,5,8,9,6,0,4,0,8},
|
||||
{0,7,1,9,8,4,0,3,8,5,0,9,6,2,4,5,5,4,4,4,3,6,2,9,8,1,2,3,0,9,8,7,8,7,9,9,2,7,2,4,4,2,8,4,9,0,9,1,8,8},
|
||||
{8,4,5,8,0,1,5,6,1,6,6,0,9,7,9,1,9,1,3,3,8,7,5,4,9,9,2,0,0,5,2,4,0,6,3,6,8,9,9,1,2,5,6,0,7,1,7,6,0,6},
|
||||
{0,5,8,8,6,1,1,6,4,6,7,1,0,9,4,0,5,0,7,7,5,4,1,0,0,2,2,5,6,9,8,3,1,5,5,2,0,0,0,5,5,9,3,5,7,2,9,7,2,5},
|
||||
{7,1,6,3,6,2,6,9,5,6,1,8,8,2,6,7,0,4,2,8,2,5,2,4,8,3,6,0,0,8,2,3,2,5,7,5,3,0,4,2,0,7,5,2,9,6,3,4,5,0}
|
||||
};
|
||||
|
||||
int row = (int)(number / 50 + 0.5);
|
||||
int column = number % 50;
|
||||
|
||||
return oneThousand[row][column];
|
||||
}
|
9
C/8-b.c
Executable file
9
C/8-b.c
Executable file
|
@ -0,0 +1,9 @@
|
|||
#include <stdio.h>
|
||||
|
||||
//testing rounding
|
||||
|
||||
void main() {
|
||||
int row = (int)(50 / 50 + 0.5);
|
||||
|
||||
printf("%d", row);
|
||||
}
|
23
C/8-c.c
Executable file
23
C/8-c.c
Executable file
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void main() {
|
||||
char oneThouseand[] = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
|
||||
|
||||
int i, k;
|
||||
int greatestProduct = 0;
|
||||
for(i = 0; i < 1000; i++) {
|
||||
if(i >= 4) {
|
||||
int currentProduct = 1;
|
||||
|
||||
for(k = i - 4; k <= i; k++) {
|
||||
currentProduct *= (oneThouseand[k] - 48);
|
||||
}
|
||||
|
||||
if(currentProduct > greatestProduct) {
|
||||
greatestProduct = currentProduct;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d", greatestProduct);
|
||||
}
|
33
C/9-a.c
Executable file
33
C/9-a.c
Executable file
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// A Pythagorean triplet is a set of three natural numbers, a b c, for which,
|
||||
|
||||
// a2^ + b^2 = c^2
|
||||
// For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
||||
|
||||
// There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||
// Find the product abc.
|
||||
|
||||
int main() {
|
||||
int n, m;
|
||||
|
||||
int a, b, c;
|
||||
|
||||
for(n = 1; n < 20; n++) {
|
||||
for(m = n + 1; m <= 20; m++) {
|
||||
a = m*m - n*n;
|
||||
b = 2*n*m;
|
||||
c = n*n + m*m;
|
||||
|
||||
printf("\n%d + %d + %d = %d", a, b, c, a + b + c);
|
||||
|
||||
if(a + b + c == 1000) goto found;
|
||||
}
|
||||
}
|
||||
|
||||
found:
|
||||
printf("\n\n%d^2 + %d^2 = %d^2", a, b, c);
|
||||
printf("\n\nProduct = %d", a * b * c);
|
||||
|
||||
return 1;
|
||||
}
|
0
C/nohup.out
Normal file
0
C/nohup.out
Normal file
10
PHP/16-b.php
Executable file
10
PHP/16-b.php
Executable file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
$POWER = 1000;
|
||||
|
||||
$sum = 0;
|
||||
|
||||
foreach(str_split(sprintf('%0.0f', pow(2, $POWER))) as $number)
|
||||
$sum += $number;
|
||||
|
||||
echo $sum;
|
11
PHP/20-b.php
Executable file
11
PHP/20-b.php
Executable file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
$factorial = 1;
|
||||
for($i = 100; $i > 0; $i--)
|
||||
$factorial = bcmul($factorial, $i);
|
||||
|
||||
$sum = 0;
|
||||
foreach(str_split($factorial) as $number)
|
||||
$sum += $number;
|
||||
|
||||
echo $sum;
|
1
README
Normal file
1
README
Normal file
|
@ -0,0 +1 @@
|
|||
Solutions for Project Euler. Solutions are numbered after the corresponding problem number with one of two things: a number that signifies the current iteration of a solution or a name when the solution is someone else's.
|
Loading…
Reference in a new issue