void findPrimes(bignum topCandidate) { BITARRAY *ba = createBitArray(topCandidate); assert(ba != NULL); /* SET ALL BUT 0 AND 1 TO PRIME STATUS */ setAll(ba); clearBit(ba, 0); clearBit(ba, 1); /* MARK ALL THE NON-PRIMES */ bignum thisFactor = 2; bignum lastSquare = 0; bignum thisSquare = 0; while (thisFactor * thisFactor <= topCandidate) { /* MARK THE MULTIPLES OF THIS FACTOR */ bignum mark = thisFactor + thisFactor; while (mark <= topCandidate) { clearBit(ba, mark); mark += thisFactor; } /* PRINT THE PROVEN PRIMES SO FAR */ thisSquare = thisFactor * thisFactor; for (; lastSquare < thisSquare; lastSquare++) { if (getBit(ba, lastSquare)) printPrime(lastSquare); } /* SET thisFactor TO NEXT PRIME */ thisFactor++; while (getBit(ba, thisFactor) == 0) thisFactor++; assert(thisFactor <= topCandidate); } /* PRINT THE REMAINING PRIMES */ for (; lastSquare <= topCandidate; lastSquare++) { if (getBit(ba, lastSquare)) printPrime(lastSquare); } freeBitArray(ba); }
void Primer::calculateNextPrime(unsigned long num) { unsigned long prime = nextPrime(); const static unsigned int PRINT_PERIOD = 100000; if (num % PRINT_PERIOD == PRINT_PERIOD - 1) { std::chrono::high_resolution_clock::time_point stopTime = std::chrono::high_resolution_clock::now(); double rate = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(stopTime - startTime).count() / (double)PRINT_PERIOD; startTime = stopTime; printPrime(num, prime, rate); } }
int main() { // test for comment testAnd(); // test for comment too testOr(); printPrime(30); printf("factorial %d,%d\n", 10, factorial(10)); printFeb1(); printFeb2(); print9x9(); perform(); printf("15-3*(2*2+(7-2)) = %d\n", 15-3*(2*2+(7-2))); printf("FALSE = %d\n", FALSE); return 0; }
/* The primeSieve function will search for the prime numbers between 2 and userInput and set them as NOT_PRIME */ void primeSieve(int userInput) { int fill, index, end, num, temp; int rangeList[userInput + 1]; /* Initialize an array of userInput + 1, because we need numbers 0 through userInput. */ printf("\n\n"); printf("Welcome!\n"); printf("We are going to sieve for all of the prime numbers between 2 and %d!\n", userInput); /* Fill the array with numbers 2 through userInput */ for (fill = 0; fill <= userInput; fill++) { rangeList[fill] = fill; } printf("\n\n"); end = sqrt(userInput); /* By taking the square root of the userInput, that's when we will stop eliminating the multiples of each number. Therefore, we don't have to go through each element in the rangeList */ printf("For sieving purposes, the approximate square root of %d is: %d\n", userInput, end); for(index = 2; index <= end; index++) /* Start at index 2 for the outer loop until the sqrt of userInput has been reached */ { num = index; /* set num to outer */ temp = num; /* and temp to num */ while(temp <= userInput) /* Continue until temp is greater than userInput */ { temp += num; /* add num to itself and set it to temp, to get the next number to set as isNotPrime or jump to */ /* Check if the value is not already a prime and temp is less thn the userInput */ if((rangeList[temp] != NOT_PRIME) && (temp <= userInput)) { rangeList[temp] = NOT_PRIME; /* Mark the value as not prime: 0 */ } } } printf("\n"); printPrime(rangeList, userInput); /* Call function to print the prime numbers */ }
void findPrimes(bignum topCandidate) { bignum candidate = 2; while(candidate <= topCandidate) { bignum trialDivisor = 2; int prime = 1; while(trialDivisor * trialDivisor <= candidate) { if(candidate % trialDivisor == 0) { prime = 0; break; } trialDivisor++; } if(prime){ printPrime(candidate); } candidate++; } }
void solve(BIT sp[], BIT p1[], BIT p2[], BIT z[], int &n, int current_position, BIT carry, int &sp_n){ //n is number of bits in p1,p2 //if(current_position == 0) solve(sp, p1, p2, z, n, current_position+1, 0); //std::cout << sp_cp << "\n"; //std::string pause; getline(std::cin, pause); //reverse(p1, n);printPrime(p1,n);reverse(p1, n); //reverse(p2, n);printPrime(p2,n);reverse(p2, n); //std::cout << "current pos: " << current_position << "\n"; if(current_position == n){ std::vector<BIT> product; multiply(p1,p2,product,n); trim(product); /* std::cout << "p1\n"; reverse(p1, n);printPrime(p1,n);reverse(p1, n); std::cout << "p2\n"; reverse(p2, n);printPrime(p2,n);reverse(p2, n); std::cout << "p1*p2\n"; reverse(product);printPrime(product);reverse(product); std::cout << "sp\n"; reverse(sp,sp_n);printPrime(sp,sp_n);reverse(sp,sp_n); */ if( areEqual(product,sp,sp_n) ){ reverse(p1, n); reverse(p2, n); reverse(product); reverse(sp,sp_n); std::cout << "Hooray!\n"; printPrime(p1,n); printPrime(p2,n); printPrime(product); printPrime(sp,sp_n); std::cout << "------\n"; std::cout << "TIME: " << float( clock () - begin_time ) / CLOCKS_PER_SEC << "\n"; exit(0); } return; } BIT sum_of_carry_and_known = carry; for(int i = 1; i < current_position; i++){ sum_of_carry_and_known += p2[i]*p1[current_position-i]; } //std::cout << sum_of_carry_and_known << "\n"; //std::cout << sp[current_position] << "\n"; if(sp[current_position] == 0){ if(sum_of_carry_and_known % 2 == 0){ p1[current_position] = 0; p2[current_position] = 0; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); p1[current_position] = 1; p2[current_position] = 1; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); } else if(sum_of_carry_and_known % 2 == 1){ p1[current_position] = 0; p2[current_position] = 1; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); p1[current_position] = 1; p2[current_position] = 0; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); } }else if(sp[current_position] == 1){ if(sum_of_carry_and_known % 2 == 0){ p1[current_position] = 0; p2[current_position] = 1; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); p1[current_position] = 1; p2[current_position] = 0; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); } else if(sum_of_carry_and_known % 2 == 1){ p1[current_position] = 0; p2[current_position] = 0; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); p1[current_position] = 1; p2[current_position] = 1; carry = (sum_of_carry_and_known+p1[current_position]+p2[current_position])/2; solve(sp,p1,p2,z,n,current_position+1,carry,sp_n); } } }