I need to write a few C++ program as a practice problem for my book that I'm learning out of (Jumping into C++). Right now I want to focus on three of them, the first of which is one where I have to find the prime factors of the first 1000 numbers and then find and the print the ones that sum up to prime numbers.
The book at already has a complete program on how to find and print the prime numbers out of the first 100 numbers. So I just have to build off of that, I guess, the problem is that I don't know how to find prime factorizations in C++.
These are the three Practice Problems I mentioned:
1. Implement the source code that turns numbers into English text.
2. Think about how you would go in the opposite direction, reading English text and translating it
into source code. Is this easier or harder than the earlier algorithm? How would you handle bad
input?
3. Design a program that finds all numbers from 1 to 1000 whose prime factors, when added
together, sum up to a prime number (for example, 12 has prime factors of 2, 2, and 3, which
sum to 7, which is prime). Implement the code for that algorithm.
Another issue I'm currently having is actually with this Practice Problem:
Write a program that provides the option of tallying up the results of a poll with 3 possible
values. The first input to the program is the poll question; the next three inputs are the possible
answers. The first answer is indicated by 1, the second by 2, the third by 3. The answers are
tallied until a 0 is entered. The program should then show the results of the poll—try making a
bar graph that shows the results properly scaled to fit on your screen no matter how many
results were entered.
This is the code I've got so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
using namespace std;
int user_chosen_option(int user_input);
int main()
{
int user_input;
cout << "What is your favorite type of music?";
cout << "Option 1: Rock\n";
cout << "Option 2: Metal\n";
cout << "Option 3: Jazz\n";
cin >> user_input;
user_chosen_option(user_input);
}
int user_chosen_option(int user_input)
{
switch(user_input)
{
case 1:
cout << "That's an interesting choice in music. "
}
}
|
And as for the program for the prime number generator for the first 100 numbers, here is the code: #include <iostream>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <math.h>
// note the use of function prototypes
bool isDivisible (int number, int divisor);
bool isPrime (int number);
using namespace std;
int main ()
{
for ( int i = 0; i < 100; i++ )
{
if ( isPrime( i ) )
{
cout << i << endl;
}
}
}
bool isPrime (int number)
{
for (int i = sqrt((double)number); i < number; i++)
{
if ( isDivisible( number, i ) )
{
return false;
}
}
return true;
}
bool isDivisible (int number, int divisor)
{
return number % divisor == 0;
}
|
The book said that using 2 as the number to start with wouldn't be efficient enough and suggested using the square-root of the number currently being checked. That's why I did "for (int i = sqrt((double)num); . . . ) for the initialization part of that for-loop. The original sample code in the book uses "for (int i = 2; . . . ) for the initialization.