Advertisement
Guest User

projecteuler-16.c

a guest
Apr 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1.  
  2. // https://projecteuler.net/problem=16
  3. //
  4. // to build:
  5. // cc -lgmp projecteuler-16.c -o projecteuler-16
  6. //
  7. // in response to:
  8. // https://boards.4channel.org/g/thread/70641542#p70645151
  9. // https://boards.4channel.org/g/thread/70641542#p70644975
  10. //
  11. // libgmp is one of the most famous BigInt libraries
  12. // gcc itself is compiled with either libgmp or mpfr
  13. // https://stackoverflow.com/questions/9450394/how-to-install-gcc-piece-by-piece-with-gmp-mpfr-mpc-elf-without-shared-libra
  14. //
  15. // the easy way to do BigInt euler problems is to code in a language with builtin BigInts,
  16. // Common Lisp, Ruby, Python, some versions of Lua (not LuaJIT), etc.
  17. //
  18. // we demonstrate a crude special purpose solution in plain C (not full implementation)
  19. // then repeat same method using GMP BigInts (probably slower actually, but only in special case).
  20.  
  21. #include <stdio.h>
  22. #include <gmp.h> // apt get libgmp-dev
  23.  
  24. #define ENOUGH 512 // 300-something would be enough...
  25.  
  26. int CrudeInt[ENOUGH] = {0};
  27.  
  28. void crude_x2() {
  29.     int tmp;
  30.     int carry = 0;
  31.     for(int i = 0; i < ENOUGH; i++) {
  32.         tmp = 2*CrudeInt[i] + carry;
  33.         carry = tmp / 10;
  34.         tmp = tmp % 10;
  35.         CrudeInt[i] = tmp;
  36.     }
  37.     return;
  38. }
  39.  
  40. int crude_sum_digits() {
  41.     int acc = 0;
  42.     for(int i = 0; i < ENOUGH; i++) acc += CrudeInt[i];
  43.     return acc;
  44. }
  45.  
  46. int main() {
  47.     // CrudeInt[] and related functions defined above
  48.     int CrudeSum;
  49.  
  50.     mpz_t GmpSum;
  51.     mpz_t BigInt;
  52.     mpz_t bigtmp;
  53.  
  54.     CrudeInt[0] = 2; // this is 2^1
  55.     for(int i = 1; i < 1000; i++) crude_x2();
  56.     CrudeSum = crude_sum_digits();
  57.  
  58.     // https://www.cs.colorado.edu/~srirams/courses/csci2824-spr14/gmpTutorial.html
  59.     // https://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test
  60.     // ...
  61.     // search for (with quotes): "#include <gmp.h>" site:pastebin.com/
  62.     // google, bing, duckduckgo support the "site:" search operator
  63.  
  64.     mpz_init(GmpSum); mpz_init(BigInt); mpz_init(bigtmp);
  65.     mpz_set_ui(BigInt, 2);
  66.     for(int i = 1; i < 1000; i++) mpz_mul_ui(BigInt, BigInt, 2);
  67.  
  68.     while(mpz_cmp_ui(BigInt, 0) != 0) {
  69.         mpz_mod_ui(bigtmp, BigInt, 10);
  70.         mpz_add(GmpSum, GmpSum, bigtmp); // all bigints this time, no "_ui" suffix
  71.         mpz_div_ui(BigInt, BigInt, 10);
  72.     }
  73.  
  74.     printf("CrudeSum: %d\n", CrudeSum);
  75.     gmp_printf("GmpSum  : %Zd\n", GmpSum);
  76.  
  77.     mpz_clear(GmpSum); mpz_clear(BigInt); mpz_clear(bigtmp);
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement