Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- int count_odds(int prime);
- int count_evens(int prime);
- bool isprime(int test);
- int main()
- {
- int total_odds = 1; //this value starts at one because we aren't testing the value, 3.
- int total_evens = 1; //this value starts at one because we aren't testing the value, 2.
- int total_primes = 2;
- int upper_limit;
- cout << "until what value should I test primes?" << endl;
- cin >> upper_limit;
- for(int i=5; i < upper_limit; i++)
- {
- if(isprime(i))
- {
- total_odds = total_odds + count_odds(i);
- total_evens = total_evens + count_evens(i);
- total_primes++;
- }
- }
- cout << "the total number of primes found was " << total_primes << endl;
- cout << "the number of even digits found was " << total_evens << endl;
- cout << "the number of odd digits found was " << total_odds << endl;
- return 0;
- }
- /*
- purpose: test for primality
- input: int test
- output bool
- */
- bool isprime(int test)
- {
- if(test % 2 == 0)
- {
- return false;
- }
- double square = sqrt(test);
- for(int i = 3; i < square + 1; i+=2)
- {
- if(test % i == 0)
- {
- return false;
- }
- }
- return true;
- }
- /*
- purpose: count the odd digits of a prime
- input : a prime number
- output : the number of odd digits of that prime
- */
- int count_odds(int prime)
- {
- int odds = 0;
- int final_digit;
- while(prime != 0)
- {
- final_digit = prime % 10;
- if(final_digit % 2 == 1)
- {
- odds++;
- }
- prime = ((prime - (prime % 10))/10);
- }
- return odds;
- }
- /*
- purpose: count the odd digits of a prime
- input : a prime number
- output : the number of odd digits of that prime
- */
- int count_evens(int prime)
- {
- int evens = 0;
- int final_digit;
- while(prime != 0)
- {
- final_digit = prime % 10;
- if(final_digit % 2 == 0)
- {
- evens++;
- }
- prime = ((prime - (prime % 10))/10);
- }
- return evens;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement