diff options
author | sijanec <sijanecantonluka@gmail.com> | 2020-12-14 00:04:39 +0100 |
---|---|---|
committer | sijanec <sijanecantonluka@gmail.com> | 2020-12-14 00:04:39 +0100 |
commit | ddff09bb8def69d35c43649736dd4c3a5f0aef7f (patch) | |
tree | 776b3e0fa9a05c6feab0b753ed21ab011ff28afd /mat/euler | |
parent | weekend reading 6 (diff) | |
parent | odstranil dependency za f5ff, domača naloga 20 pri matematiki (diff) | |
download | sola-gimb-2-ddff09bb8def69d35c43649736dd4c3a5f0aef7f.tar sola-gimb-2-ddff09bb8def69d35c43649736dd4c3a5f0aef7f.tar.gz sola-gimb-2-ddff09bb8def69d35c43649736dd4c3a5f0aef7f.tar.bz2 sola-gimb-2-ddff09bb8def69d35c43649736dd4c3a5f0aef7f.tar.lz sola-gimb-2-ddff09bb8def69d35c43649736dd4c3a5f0aef7f.tar.xz sola-gimb-2-ddff09bb8def69d35c43649736dd4c3a5f0aef7f.tar.zst sola-gimb-2-ddff09bb8def69d35c43649736dd4c3a5f0aef7f.zip |
Diffstat (limited to 'mat/euler')
-rw-r--r-- | mat/euler/15/NEDOKONCAN | 0 | ||||
-rw-r--r-- | mat/euler/16/Makefile | 2 | ||||
-rwxr-xr-x | mat/euler/16/a.out | bin | 0 -> 8600 bytes | |||
-rw-r--r-- | mat/euler/16/mnozi.c | 31 | ||||
-rw-r--r-- | mat/euler/16/prog.c | 30 | ||||
-rw-r--r-- | mat/euler/17/Makefile | 4 | ||||
-rwxr-xr-x | mat/euler/17/a.out | bin | 0 -> 13192 bytes | |||
-rw-r--r-- | mat/euler/17/angnum.c | 148 | ||||
-rw-r--r-- | mat/euler/17/prog.c | 28 |
9 files changed, 243 insertions, 0 deletions
diff --git a/mat/euler/15/NEDOKONCAN b/mat/euler/15/NEDOKONCAN new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mat/euler/15/NEDOKONCAN diff --git a/mat/euler/16/Makefile b/mat/euler/16/Makefile new file mode 100644 index 0000000..8278251 --- /dev/null +++ b/mat/euler/16/Makefile @@ -0,0 +1,2 @@ +default: + gcc prog.c -pedantic -I. diff --git a/mat/euler/16/a.out b/mat/euler/16/a.out Binary files differnew file mode 100755 index 0000000..25772a9 --- /dev/null +++ b/mat/euler/16/a.out diff --git a/mat/euler/16/mnozi.c b/mat/euler/16/mnozi.c new file mode 100644 index 0000000..f90cdbd --- /dev/null +++ b/mat/euler/16/mnozi.c @@ -0,0 +1,31 @@ +#pragma once +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +int mnozi (unsigned long long int mnozi_s_tem, char * d, long int b) { + unsigned long long int carry = 0; // carry je pri množenju vedno pozitiven + unsigned long long int to = 0; + unsigned long long int za_napisati = 0; + for (b = b-1; b >= 0; b--) { + to = d[b] - '0'; + za_napisati = to * mnozi_s_tem; + za_napisati = za_napisati + carry; + carry = 0; + if (za_napisati > 9) { + za_napisati = za_napisati - 10; + carry++; + } + d[b] = za_napisati + '0'; + } + return 0; +} +#ifndef DISABLE_LIB_TESTS +int main (int argc, char ** argv) { + unsigned long long int m = 2; + char d[] = "0000000000000000000000000000000000000000000000000000456"; + int b = strlen(d); + mnozi(m, d, b); + fprintf(stdout, "test: %s\n", d); + return 0; +} +#endif diff --git a/mat/euler/16/prog.c b/mat/euler/16/prog.c new file mode 100644 index 0000000..5a2d7f3 --- /dev/null +++ b/mat/euler/16/prog.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <stdlib.h> +#define DISABLE_LIB_TESTS +#include <mnozi.c> +#define MAX_SIRINA 80*(24-3) /* zapolnimo malo teminalsko okno */ +#define MAX_SIRINA_S "1680" /* = 80 * 21; nastavi tudi to za format stavek */ +int main (int argc, char ** argv) { + if (argc != 1+2) { + fprintf(stderr, "uporaba: %s <Številka> (^) <Številka>\nprimer za izračun 2^1000: %s 2 1000\n", argv[0], argv[0]); + return 1; + } + unsigned long long int n = strtoull(argv[1], NULL, 10); + unsigned long long int m = strtoull(argv[2], NULL, 10); + if (m == 0) { + fprintf(stderr, "KARKOLI NA NIČ JE ENA! (reci to računalniku)\n"); + return 2; + } + char * z = malloc(sizeof(char)*(MAX_SIRINA + 1)); // + 1 za \0 + unsigned int l = MAX_SIRINA; + unsigned long long int s = 0; // Seštevek števk + sprintf(z, "%0" MAX_SIRINA_S "llu", n); + z[MAX_SIRINA] = '\0'; + for (; m > 1; m--) + // fprintf(stderr, "\rpreostane še %llu operacij ...", m); + mnozi (n, z, l); + for (m = 0; m < MAX_SIRINA; m++) // sicer je m itak že 0 ampak okej + s = s + (z[m] - '0'); + fprintf(stdout, "\rkonec računanja. seštevek števk v rezultatu je %llu, število je\n%s\n", s, z); + return 0; +} diff --git a/mat/euler/17/Makefile b/mat/euler/17/Makefile new file mode 100644 index 0000000..dff62a0 --- /dev/null +++ b/mat/euler/17/Makefile @@ -0,0 +1,4 @@ +default: + gcc -I. prog.c -pedantic -lm +libtest: + gcc -I. angnum.c -pedantic -lm diff --git a/mat/euler/17/a.out b/mat/euler/17/a.out Binary files differnew file mode 100755 index 0000000..e83fc19 --- /dev/null +++ b/mat/euler/17/a.out diff --git a/mat/euler/17/angnum.c b/mat/euler/17/angnum.c new file mode 100644 index 0000000..2f16d75 --- /dev/null +++ b/mat/euler/17/angnum.c @@ -0,0 +1,148 @@ +#pragma once +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +/* definiramo jezik - ZAČETEK */ +#define J_0 "zero" +#define J_1 "one" +#define J_2 "two" +#define J_3 "three" +#define J_4 "four" +#define J_5 "five" +#define J_6 "six" +#define J_7 "seven" +#define J_8 "eight" +#define J_9 "nine" +#define J_10 "ten" +#define J_11 "eleven" +#define J_12 "twelve" +#define J_13 "thirteen" +#define J_14 "fourteen" +#define J_15 "fifteen" +#define J_16 "sixteen" +#define J_17 "seventeen" +#define J_18 "eighteen" +#define J_19 "nineteen" +#define J_20 "twenty" +#define J_30 "thirty" +#define J_40 "forty" +#define J_50 "fifty" +#define J_60 "sixty" +#define J_70 "seventy" +#define J_80 "eighty" +#define J_90 "ninety" +#define J_1h "hundred" +#define J_1k "thousand" +#define J_1M "million" +#define J_1G "billion" +#define J_1T "trillion" +#define J_1P "quadrillion" +#define J_1E "quintillion" +#define J_1Z "sextillion" +#define J_1Y "septillion" +#define J_AND "and" +#define J_NEG "negative" +#define J_EMPTY "" +/* definiramo jezik - KONEC */ +#define MX_NS_L 1337 +char * j_enice[] = { J_EMPTY, J_1, J_2, J_3, J_4, J_5, J_6, J_7, J_8, J_9, J_10, J_11, J_12, J_13, J_14, J_15, J_16, J_17, J_18, J_19, J_20 }; +char * j_power[] = { J_EMPTY, J_1k, J_1M, J_1G, J_1T, J_1P, J_1E, J_1Z, J_1Y }; +char * j_desetice[] = { J_EMPTY, J_10, J_20, J_30, J_40, J_50, J_60, J_70, J_80, J_90 }; + +unsigned long long int na(unsigned long long int a, unsigned long long int b) { + unsigned long long int c = 1; /* jebeš math.h ke nima funkcije za intager */ + for (; b > 0; b--) { /* potenciranje */ + c = c * a; + } + return c; +} + +/* private_angnum : size_t (število zapisanih bajtov, -1 ob propadu sveta) + * n: lluint: številka, ki naj jo zapišemo + * s: pointer to allocatanega spomina, kamor zapišemo string + * l: število bajtov allocatanega spomina za string, IZključno zadnjega '\0' + * alocirati morate vsaj en bajt več od l + * string bo mogoče avtomatsko null terminiran, vseeno ga raje dajte še sami + * UPORABLJAJTE RAJE angnum NAMESTO private_angnum */ +size_t private_angnum (unsigned long long int n, char * s, int l) { + unsigned int i = 0; // Index Iteracij + int z = 0; // Zapisanih + if (n <= 20) { // "thirteen", namesto 0 je prazen string + z = snprintf(s, l, "%s", j_enice[n]); + return z; + } + if (n <= 99) { // && n > 20 // "forty-one" ali "thirty" + z = snprintf(s, l, "%s", j_desetice[n / 10]); // intdiv je floor + if (z < l && n % 10 != 0) s[z++] = '-'; + return z + (n % 10 != 0 ? snprintf(s+z, l-z, "%s", j_enice[n % 10]) : 0); + } + if (n <= 999) { // && n > 99 + z = snprintf(s, l, "%s ", j_enice[n/100]); // "one " + z = z + snprintf(s+z, l-z, "%s", J_1h); // "hundred" + if (n % 100 != 0) { + if (z < l) s[z++] = ' '; + z = z + snprintf(s+z, l-z, "%s ", J_AND); // "and " + return z + private_angnum(n-(n/100)*100, s+z, l-z); + } else { + return z; + } + } // sedaj je n > 99 && n < 1e21 + for (i = 3; i <= 21; i=i+3) // sedaj iščemo največjo tisočico + if (n / na(10, i) <= 999) // zadnja tisočica + break; + z = private_angnum((n/na(10, i)), s, l-z); // "three hundred forty-two" + z = z + snprintf(s+z, l-z, " %s", j_power[i/3]); // "million" + if (n-(n/na(i, 10)) > 0) { + if (z < l) s[z++] = ' '; + z = z + private_angnum(n-(n/na(10, i)*na(10, i)), s+z, l-z); + } + return z; +} +/* + * angnum(n, s, sizeof) : size_t napisanih bajtov, vključno z nul bajtom + * n je celo, ki ga je treba napisati. lahko je 0, negativno ali pozit.. + * s je pointer do char, kamor naj se piše številka. + * sizeof je velikost alocatanega spomina za s, vključno z nul bajtom, naj bo>0. + * funkcija vrne število napisanih bajtov ali pa 0 v primeru napake. + * če je vrednost 0, je vsebina spomina od s do s+sizeof neopredeljena. + * število se zapiše v britanskem formatu, s pomišljajem in and. + * niz je končan z nul bajtom in pripravljen na tiskanje. + * četudi je string uspešno napisan, lahko funkcija odvrne 0, če se bliža + * omejitvi prostora sizeof. prav tako, četudi je dovolj prostora, + * lahko funkcija napiše napačno vrednost v s, če se bliža omejitvi, zato je + * nastavljen check, da funkcija odvrne 0, če je omejitev prostora 10+-2 + * bajtov stran od napisanega. + * funkcija naj ne bi pisala preko omejitve sizeof. + * */ +size_t angnum (long long int n, char * s, int l) { + size_t z = 0; + size_t i = 0; + if (n == 0) { + z = snprintf(s, l, "%s", J_0); + return z; + } + if (n < 0) { + z = snprintf(s, l, "%s", J_NEG); + if (z < l) s[z++] = ' '; + z = z + private_angnum(abs(n), s+z, (l-z)-1); + } else { + z = z + private_angnum(n, s+z, (l-z)-1); + } + if (z < l) s[z++] = '\0'; + if (l - z < 10) + return 0; + return z; +} +#ifndef DISABLE_LIB_TESTS +int main (int argc, char ** argv) { + if (argc != 1+1) { + fprintf(stderr, "uporaba: %s <stevilka>\nprimer, ki napiše \"four hundred and twenty\": %s 420\n", argv[0], argv[0]); + return 1; + } + long long int n = strtoll(argv[1], NULL, 10); + char * s = malloc(sizeof(char)*MX_NS_L); + size_t r = angnum(n, s, MX_NS_L); + fprintf(stdout, "%s\n", s); + return r == 0 ? 2 : 0; +} +#endif diff --git a/mat/euler/17/prog.c b/mat/euler/17/prog.c new file mode 100644 index 0000000..afa005d --- /dev/null +++ b/mat/euler/17/prog.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <stdlib.h> +#define DISABLE_LIB_TESTS 1 +#include <angnum.c> +int main (int argc, char ** argv) { + if (argc != 1+2) { + fprintf(stderr, "uporaba: %s <stevilka>\nprimer-sešteje črke v ang številih od 1 do 1000: %s 1 1000\n", argv[0], argv[0]); + return 1; + } + long long int a = strtoll(argv[1], NULL, 10); + long long int b = strtoll(argv[2], NULL, 10); + unsigned long long int i = 0; // lenasI + long long int j = 0; // Janez je podpisan, da se po 0 ne obrne (: + unsigned long long int e = 0; // sEštevek + char * s = malloc(sizeof(char)*MX_NS_L); + for (i = a; i <= b; i++) { + // fprintf(stderr, "\rračunam ... preostane še %llu operacij.", b-i); + j = angnum(i, s, MX_NS_L); + if (j == 0) + return 2; + for (j-- /* da ne gledamo nul bajta na koncu (: */ ; j >= 0; j--) { + if (s[j] >= 'a' && s[j] <= 'z') // that's why I like C + e++; + } + } + fprintf(stdout, "\rkonec. seštevek črk je %llu.\n", e); + return 0; +} |