/ Published in: C++
all better?
Expand |
Embed | Plain Text
//Patrick Liggett Assignment #5 CS161 //This program calculates the number of primes in the first 50 chiliads and the average per chiliad. //Sources: Nick Minkler- friend. Helped with the algorthim. #include <iostream> #include <cmath> #include <iomanip> using namespace std; bool isPrime (long); long primeCount (long x, long y); int main() { long count = 0; long totalCount = 0; cout << setw(10) << "Start" << "End" << "Number of Primes" << endl; for(long i = 0; i <= 49; i++) { count = primeCount (1 + (i * 1000), (i + 1) * 1000); totalCount += count; cout << count << i + 1; cout << 1 + i * 1000 << setw(10) << " " << (i + 1) * 1000 << " " << count; } return 0; } bool isPrime (long n) { bool prime; prime = true; if(n % 2 == 0) prime = false; for(long i = 3; i < n && prime == true; i += 2) { if(n % i == 0) prime = false; } return prime; } long primeCount (long x, long y) { long count = 0; for(x; x < y; x++) { if(isPrime(x)) count++; } return count; }
You need to login to post a comment.
