summaryrefslogtreecommitdiffstats
path: root/mat/euler/13/dodaj.c
diff options
context:
space:
mode:
Diffstat (limited to 'mat/euler/13/dodaj.c')
-rw-r--r--mat/euler/13/dodaj.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/mat/euler/13/dodaj.c b/mat/euler/13/dodaj.c
new file mode 100644
index 0000000..d1fbb59
--- /dev/null
+++ b/mat/euler/13/dodaj.c
@@ -0,0 +1,38 @@
+#pragma once
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+int dodaj (const char * c, char * d, long int a, long int b) {
+ unsigned long long int carry = 0; // carry je pri seštevanju vedno pozitiven
+ long int diff = b-a;
+ unsigned long long int dodajmo_tole = 0;
+ unsigned long long int temu = 0;
+ for (b = b-1; b >= 0; b--) {
+ if (b-diff >= 0) {
+ dodajmo_tole = c[b-diff] - '0';
+ } else {
+ dodajmo_tole = 0;
+ }
+ temu = d[b] - '0';
+ dodajmo_tole = dodajmo_tole + carry;
+ carry = 0;
+ if (temu + dodajmo_tole > 9) {
+ dodajmo_tole = (temu + dodajmo_tole) - 10;
+ temu = 0;
+ carry++;
+ }
+ d[b] = temu + dodajmo_tole + '0';
+ }
+ return 0;
+}
+#ifndef DISABLE_LIB_TESTS
+int main (int argc, char ** argv) {
+ char c[] = "37107287533902102798797998220837590246510135740250";
+ char d[] = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000";
+ int a = strlen(c);
+ int b = strlen(d);
+ dodaj(c, d, a, b);
+ fprintf(stdout, "test: %s\n", d);
+ return 0;
+}
+#endif