I am known for disappearing from the internet, and not posting on my blog for long periods of time. The cause of this: first year university.
One of my classes, involves basic level C++ programming. I am absolutely captivated by something I used to avoid. I always though of myself as a hardware guy, but programming is a growing interest.
Last week, I designed a program that could calculate all prime numbers within a range specified by the user, and then output them on the screen. Running 5 instances of this program on my laptop, while calculating all prime numbers from one to a billion, actually puts quite a load on the CPU cores of my llano laptop.
Here’s a shot of a more normal input:
If anyone is interested, here is my code:
/*
Author: Matthew Sembinelli / Slappa
Created: October 22th 2011
Objective: To create a program that calculates
all prime numbers between 1 and a user
entered integer using nested for loops.
Topic: Nested Loops
*/// declare necessary libraries, using standard namespace
#include <iostream>
#include <conio.h>
using namespace std;int main ()
{
char response = ‘y’;
while (response == ‘y’)
{
// declare integer variables, as prime numbers only include positive integers
long int range = 0;
int isprime = 1;
long int count = 0;// prompt user to enter upper bound of range
cout << “Show all prime numbers between 1 and the following number: “;
cin >> range;
cout << ” ” << endl;// iterates range from the upper bound going down until range is equal to 2
for(long int r = range; r >= 2; –r)
{
/* In the next for-loop, the variable “p” is presented as a test factor for each test number in the
range. Therefore if a test number “r” is divisible (without remainder)by a
test factor in the range of 2 and the test factor squared (highest possible factor of a number)
then it is not prime. This is because that specific test factor is an actual factor of the
test number, and the factor is not equal to one or the test number itself. This is repeated
for every possible integer factor in the range of 2 and the test factor squared.
If the number is found to be non-prime, the cycle is broken and the calculation for
the next test number is done.
*/
for(long int p = 2; p * p <= r; ++p)
{
if(r % p == 0)
{
isprime = 0;
break;
}
}// if the number is not satisfied by the non-prime loop above, it is assumed prime by the following statement
if(isprime == 1)
{
// the prime number is printed to the user
cout << r << endl;
count++;
}/* If a test number is found to be non-prime, a number will not be printed.
Next, “isprime” is updated to 1 for the next loop around in the parent for-loop
or else the following prime numbers after the first loop will not be displayed.
*/
else
{
isprime = 1;
}
}// output message to user indicating all prime numbers in the range were found
cout << ” ” << endl
<< “All prime numbers were found in the range of 1 to ” << range << endl
<< “There were ” << count << ” prime numbers found.” << endl
<< ” ” << endl;
cout << “Do you wish to calculate more prime numbers? Type y or n. “;
cin >> response;
cout << ” ” << endl;
}// end program
return 0;
}


