int main() { using primes = cs477::future<std::vector<int>>; bool quit = false; clock_t t; const unsigned int NUM_VALUES = 10'000'000; const unsigned int NUM_THREADS = std::thread::hardware_concurrency() * 3; const unsigned int SECTION_SIZE = NUM_VALUES / NUM_THREADS; int temp = 0; //used to get block range std::vector<primes> f; t = clock(); for (unsigned int i = 0; i < NUM_THREADS; ++i) { unsigned int first = temp; //set temp as top of the range and compensate for a remainder from dividing into blocks temp += SECTION_SIZE + (i < (NUM_VALUES % SECTION_SIZE) ? 1 : 0); unsigned int last = temp; auto section = cs477::queue_work([i, first, last] { std::vector<int> v; for (unsigned int k = first; k < last; ++k) { if (is_prime(k)) v.push_back(k); } return v; }); std::cout << "Thread " << std::setw(get_num_digits(NUM_THREADS)) << std::left << i << ": " << std::setw(get_num_digits(NUM_VALUES) - 1) << std::right << first << " - " << std::setw(get_num_digits(NUM_VALUES)) << std::right << last << "\n"; f.push_back(std::move(section)); } printf("\n"); auto all = when_all(f.begin(), f.end()); int sec = 0; int sum = 0; int prev = 0; int total = 0; //set through each section to get section totals and overall total for (auto&& section : all.get()) { prev = sum; for (auto i : section.get()) { sum++; total++; } //reset from each section to get correct sum sum -= prev; std::cout << "Thread " << std::setw(get_num_digits(NUM_THREADS)) << std::left << sec++ << ": " << sum << " primes" << std::endl; } t = clock() - t; printf("\nThere are %d total prime numbers under %d\nTime: %f seconds\n\nQuit?(y/n): ", total, NUM_VALUES, ((float)t) / CLOCKS_PER_SEC); return 0; }
int main(){ std::set<unsigned long> products; unsigned long result = 0; //number of digits total must be 9 //each part of the identity has at least 1 and at most 7 digits //multiplicand has at most 7 digits for(unsigned long multiplicand = 1; multiplicand < 10000000; ++multiplicand){ int multiplicand_digits = get_num_digits(multiplicand); //digits of multiplier depends on digits of multiplicand and product (which is not known yet) for(unsigned long multiplier = 1; multiplier < static_cast<unsigned long>(pow(10,8-multiplicand_digits)); ++multiplier){ int multiplier_digits = get_num_digits(multiplier); int num_digits = multiplicand_digits + multiplier_digits + (int) (log10(multiplicand) + log10(multiplier) + 1); if( num_digits < 9 ){ continue; } if( num_digits > 9 ){ break; } unsigned long product = multiplicand * multiplier; std::array<bool, 10> digit_table; digit_table.fill(false); //add digits to digit table. if a digit was already added, continue to next loop if(!add_digits(multiplicand, digit_table)){ continue; } if(!add_digits(multiplier, digit_table)){ continue; } if(!add_digits(product, digit_table)){ continue; } //at this point, there are 9 digits in the identity and each one was added //also there was no zero added, so there must be all digits in the sum if(products.find(product) == products.end()){ result += product; products.insert(product); } } } std::cout << result << std::endl; return 0; }
bool is_palindrome( int n ){ int k = get_num_digits( n ); for( int i=0; i < k/2; ++i){ if( get_ith_digit( n, i ) != get_ith_digit( n, k-1-i ) ) return false; } return true; }
bool add_digits(unsigned long n, std::array<bool, 10>& digit_table){ int num_digits = get_num_digits(n); for(int i = 0; i < num_digits; ++i){ int digit = get_ith_digit(n, i); if( digit_table[digit] || digit == 0){ return false; } else{ digit_table[digit] = true; } } return true; }
int is_palindrome(long n) { int num_digits = get_num_digits(n); char str[num_digits]; sprintf(str, "%ld", n); char *start = str; char *end = str+strlen(str)-1; while (start < end) { if (*start != *end) return 0; ++start; --end; } return 1; }
long reverse(long n) { int num_digits = get_num_digits(n); char str[num_digits]; sprintf(str, "%ld", n); char *start = str; char *end = str+strlen(str)-1; while(start < end) { char temp = *end; *end = *start; *start = temp; ++start; --end; } return atol(str); }
int main () { FILE *fin = fopen ("zerosum.in", "r"); FILE *fout = fopen ("zerosum.out", "w"); int N; fscanf(fin, "%d", &N); int max_value = (int) pow(3.0, N - 1); int i; for(i = 0; i < max_value; i++) { int temp = i; int* ops; int op_count = 0; ops = malloc(sizeof(int) * (N)); int result = 0; int prev_num = 0; int prev_op = -1; int curr_op; int j; for(j = N; j >= 1; j--) { if(j > 1) { curr_op = temp % 3; temp /= 3; } else { curr_op = -1; } if(prev_op == 0) { prev_num = j * ((int) pow(10, get_num_digits(prev_num))) + prev_num; } else if(prev_op == -1) { prev_num = j; } else if(prev_op == 1) { result += prev_num; prev_num = j; } else { result -= prev_num; prev_num = j; } prev_op = curr_op; ops[op_count] = curr_op; op_count++; } result += prev_num; if(result == 0) { for(j = 1; j <= N; j++) { if(j == 1) { fprintf(fout, "%d", j); } else { char op = ' '; if(ops[op_count - j] == 0) { op = ' '; } else if(ops[op_count - j] == 1) { op = '+'; } else { op = '-'; } fprintf(fout, "%c%d", op, j); } } fprintf(fout, "\n"); } free(ops); } fclose(fin); fclose(fout); return 0; }