char *my_modulo(char *n1, char *n2) { t_div *d; d = init_my_mod(n1, n2); if ((d != NULL) && (my_str_isnum(d->dvd) == 1) && (my_str_isnum(d->dvs) == 1)) if (d->dvs[0] != '0') { while (d->dvd[0] != '-') { if ((my_do_op_sub_cmp(d->dvs, d->dvd, 0, 0) == 0) && (d->mod[0] == '0')) my_strcpy(d->mod, d->dvd); my_strcpy(d->temp_cmp, d->dvs); my_strcpy(d->temp_fact, "1"); while (my_do_op_sub_cmp(d->temp_cmp, d->dvd, 0, 0) == 1) add_zero(d); (check_digits(d) == 1) ? (add_zero(d)) : (1); remove_zero(d); if (check_digits(d) == 1) d->temp_fact = add_sub(d->temp_fact, "-1"); get_minus(d->temp_cmp); d->total = add_sub(d->total, d->temp_fact); d->dvd = add_sub(d->dvd, d->temp_cmp); } free(d); return (check_neg(d, d->mod, 2, 0)); } else my_putstr(DIV_0); return (0); }
int problem32(int argc, char** argv) { int i, j, k; int cnt = 0; int sum = 0; int i_cnt, j_cnt, r_cnt; int pandigitals[20] = { 0 }; for (i = 1; i < 9999; ++i) { i_cnt = digit_count(i); for (j = i+1; j < 9999; ++j) { j_cnt = digit_count(j); r_cnt = digit_count(i*j); if (i_cnt + j_cnt + r_cnt > 9) break; if (i_cnt + j_cnt + r_cnt < 9) continue; if (check_digits(i, j, i*j)) { int found = 0; for (k = 0; k < 20 && pandigitals[k]; ++k) { if (pandigitals[k] == (i*j)) { found = 1; break; } } if (!found) { printf("Pandigital found: %d * %d = %d\n", i, j, i*j); cnt++; pandigitals[k] = i * j; sum += (i * j); } else { printf("Duplicate found: %d\n", i*j); } } } } printf("Count: %d\n", cnt); printf("Sum: %d\n", sum); return(0); }
int main() { check_digits(112); check_digits(314); check_digits(3114); }
int main(int argc, char **argv) { mpfr_t tmp1; mpfr_t tmp2; mpfr_t tmp3; mpfr_t s1; mpfr_t s2; mpfr_t r; mpfr_t a1; mpfr_t a2; time_t start_time; time_t end_time; // Parse command line opts int hide_pi = 0; if(argc == 2) { if(strcmp(argv[1], "--hide-pi") == 0) { hide_pi = 1; } else if((precision = atoi(argv[1])) == 0) { fprintf(stderr, "Invalid precision specified. Aborting.\n"); return 1; } } else if(argc == 3) { if(strcmp(argv[1], "--hide-pi") == 0) { hide_pi = 1; } if((precision = atoi(argv[2])) == 0) { fprintf(stderr, "Invalid precision specified. Aborting.\n"); return 1; } } // If the precision was not specified, default it if(precision == 0) { precision = DEFAULT_PRECISION; } // Actual number of correct digits is roughly 3.35 times the requested precision precision *= 3.35; mpfr_set_default_prec(precision); mpfr_inits(tmp1, tmp2, tmp3, s1, s2, r, a1, a2, NULL); start_time = time(NULL); // a0 = 1/3 mpfr_set_ui(a1, 1, MPFR_RNDN); mpfr_div_ui(a1, a1, 3, MPFR_RNDN); // s0 = (3^.5 - 1) / 2 mpfr_sqrt_ui(s1, 3, MPFR_RNDN); mpfr_sub_ui(s1, s1, 1, MPFR_RNDN); mpfr_div_ui(s1, s1, 2, MPFR_RNDN); unsigned long i = 0; while(i < MAX_ITERS) { // r = 3 / (1 + 2(1-s^3)^(1/3)) mpfr_pow_ui(tmp1, s1, 3, MPFR_RNDN); mpfr_ui_sub(r, 1, tmp1, MPFR_RNDN); mpfr_root(r, r, 3, MPFR_RNDN); mpfr_mul_ui(r, r, 2, MPFR_RNDN); mpfr_add_ui(r, r, 1, MPFR_RNDN); mpfr_ui_div(r, 3, r, MPFR_RNDN); // s = (r - 1) / 2 mpfr_sub_ui(s2, r, 1, MPFR_RNDN); mpfr_div_ui(s2, s2, 2, MPFR_RNDN); // a = r^2 * a - 3^i(r^2-1) mpfr_pow_ui(tmp1, r, 2, MPFR_RNDN); mpfr_mul(a2, tmp1, a1, MPFR_RNDN); mpfr_sub_ui(tmp1, tmp1, 1, MPFR_RNDN); mpfr_ui_pow_ui(tmp2, 3UL, i, MPFR_RNDN); mpfr_mul(tmp1, tmp1, tmp2, MPFR_RNDN); mpfr_sub(a2, a2, tmp1, MPFR_RNDN); // s1 = s2 mpfr_set(s1, s2, MPFR_RNDN); // a1 = a2 mpfr_set(a1, a2, MPFR_RNDN); i++; } // pi = 1/a mpfr_ui_div(a2, 1, a2, MPFR_RNDN); end_time = time(NULL); mpfr_clears(tmp1, tmp2, tmp3, s1, s2, r, a1, NULL); // Write the digits to a string for accuracy comparison char *pi = malloc(precision + 100); if(pi == NULL) { fprintf(stderr, "Failed to allocated memory for output string.\n"); return 1; } mpfr_sprintf(pi, "%.*R*f", precision, MPFR_RNDN, a2); // Check out accurate we are unsigned long accuracy = check_digits(pi); // Print the results (only print the digits that are accurate) if(!hide_pi) { // Plus two for the "3." at the beginning for(unsigned long i=0; i<(unsigned long)(precision/3.35)+2; i++) { printf("%c", pi[i]); } printf("\n"); } // Send the time and accuracy to stderr so pi can be redirected to a file if necessary fprintf(stderr, "Time: %d seconds\nAccuracy: %lu digits\n", (int)(end_time - start_time), accuracy); mpfr_clear(a2); free(pi); pi = NULL; return 0; }