Created
November 9, 2023 02:09
-
-
Save MarekKnapek/e8705d9d3a48995034eced22f0454144 to your computer and use it in GitHub Desktop.
Five Million Digit Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* you need my library from */ | |
/* https://github.com/MarekKnapek/mk_clib */ | |
/* https://www.reddit.com/r/C_Programming/comments/17qgg6u/storing_extremely_large_numbers_in_c/ */ | |
#define mk_lang_jumbo_want 1 | |
#include "mk_lang_charbit.h" | |
#include "mk_lang_sizeof.h" | |
#define mk_lang_bui_t_name big_num_base | |
#define mk_lang_bui_t_base ullong | |
#include "mk_lang_bui_inl_fileh.h" | |
#include "mk_lang_bui_inl_filec.h" | |
#define big_num_base_sizebits_d (mk_lang_sizeof_bi_ullong_t * mk_lang_charbit) | |
#define mk_sl_cui_t_name big_num | |
#define mk_sl_cui_t_base big_num_base | |
#define mk_sl_cui_t_count 100 /* <== put 259526 here instead */ | |
#include "mk_sl_cui_inl_fileh.h" | |
#include "mk_sl_cui_inl_filec.h" | |
#include <assert.h> /* asseert */ | |
#include <stdio.h> /* printf */ | |
#include <string.h> /* strlen */ | |
static char const number_a[] = "4704745878477872019277152807317679077071572134447306057007334924369311383504931631284042512192565179"; | |
static char const number_b[] = "2909285452011658393419656213491434159562586586557055269049652098580338507224264829397285847831630577"; | |
int main(int argc, char** argv) | |
{ | |
big_num_t a; /* beware, you need large stack, consult your linker manual */ | |
big_num_t b; | |
big_num_t c; | |
char str[big_num_strlendec_v + 1]; | |
int n; | |
n = big_num_from_str_dec_n(&a, number_a, (int)strlen(number_a)); assert(n == strlen(number_a)); | |
n = big_num_from_str_dec_n(&b, number_b, (int)strlen(number_b)); assert(n == strlen(number_b)); | |
big_num_add3_wrap_cid_cod(&a, &b, &c); | |
n = big_num_to_str_dec_n(&c, str, big_num_strlendec_v); assert(n >= 1); str[n] = '\0'; | |
printf("%s\n", str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment