Advertisement
Guest User

potatoes

a guest
Jul 31st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *strrev(char *str) {
  6.     char *end = str + strlen(str) - 1;
  7.     char *begin = str;
  8.     while (begin < end) {
  9.         *begin ^= *end;
  10.         *end ^= *begin;
  11.         *begin ^= *end;
  12.  
  13.         begin++;
  14.         end--;
  15.     }
  16.     return str;
  17. }
  18.  
  19. char *uint128tostr(__uint128_t n) {
  20.     char *b = malloc(64 * sizeof(char));
  21.     if (n == 0) {
  22.         b[0] = '0';
  23.         b[1] = 0;
  24.         return b;
  25.     }
  26.     unsigned int i = 0;
  27.     while (n) {
  28.         b[i++] = n % 10 + '0';
  29.         n /= 10;
  30.     }
  31.     b[i] = 0;
  32.     return strrev(b);
  33. }
  34.  
  35. __uint128_t frombase(__uint128_t num, __uint128_t radix) { /* from base 2 */
  36.     if (radix == 2) return num;
  37.     __uint128_t n = 1;
  38.     __uint128_t ret = 0;
  39.     while (num) {
  40.         __uint128_t digit = num % 2;
  41.         num /= 2;
  42.         ret += digit * n;
  43.         n *= radix;
  44.     }
  45.     return ret;
  46. }
  47.  
  48. __uint128_t isprime(__uint128_t num) { /* returns 1 if prime, 0 if the number is 1, an factor otherwise */
  49.     if (num == 1) return 0;
  50.     if (num % 2 == 0) return 2;
  51.     if (num % 3 == 0) return 3;
  52.     if (num % 5 == 0) return 5;
  53.     if (num % 7 == 0) return 7;
  54.     if (num % 11 == 0) return 11;
  55.     if (num < 11*11) return 1;
  56.  
  57.     __uint128_t i;
  58.     for (i = 13; i*i <= num && i < 5000000; i += 2) { // just give up if you're trying numbers over 5000000 :P
  59.         if (num % i == 0) return i;
  60.     }
  61.  
  62.     return 1;
  63. }
  64.  
  65. __uint128_t *isjamcoin(__uint128_t jamcoin) { /* returns list of divisors if is a jamcoin, NULL otherwise */
  66.     __uint128_t *divisors = malloc(9 * sizeof(__uint128_t));
  67.  
  68.     __uint128_t i;
  69.     for (i = 2; i <= 10; i++) {
  70.         __uint128_t n = frombase(jamcoin, i);
  71.         __uint128_t r = isprime(n);
  72.         if (r > 1) {
  73.             divisors[i-2] = r;
  74.         } else {
  75.             free(divisors);
  76.             return NULL;
  77.         }
  78.     }
  79.  
  80.     return divisors;
  81. }
  82.  
  83. void printjamcoins(unsigned int length, unsigned int num) {
  84.     unsigned int count = 0;
  85.     __uint128_t template = ((__uint128_t) 1 << (length-1)) + 1;
  86.     __uint128_t i = 0;
  87.     while (count < num) {
  88.         __uint128_t jamcoin = template + ((__uint128_t) i << 1);
  89.         __uint128_t *ret = isjamcoin(jamcoin);
  90.         if (ret) {
  91.             int j;
  92.             for (j = length-1; j >= 0; j--) {
  93.                 printf("%d", (jamcoin & ((__uint128_t) 1 << j)) ? 1 : 0);
  94.             }
  95.             printf(" %s %s %s %s %s %s %s %s %s\n",
  96.                     uint128tostr(ret[0]), uint128tostr(ret[1]),
  97.                     uint128tostr(ret[2]), uint128tostr(ret[3]),
  98.                     uint128tostr(ret[4]), uint128tostr(ret[5]),
  99.                     uint128tostr(ret[6]), uint128tostr(ret[7]),
  100.                     uint128tostr(ret[8]));
  101.             /*printf(" %lu %lu %lu %lu %lu %lu %lu %lu %lu", ret[0], ret[1], ret[2], ret[3], ret[4], ret[5], ret[6], ret[7], ret[8]);*/
  102.             count++;
  103.         }
  104.         i++;
  105.     }
  106. }
  107.  
  108. int main() {
  109.     int _, n, j;
  110.     scanf("%d\n%d %d", &_, &n, &j);
  111.     printf("Case #1:\n");
  112.     printjamcoins(n, j);
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement