Very Simple Prime Number Finder v1.8

kelechi96 0 Tallied Votes 231 Views Share

Hi this basicly finds and stores prime numbers at you will need to put a file called startnumber.ini with a number in it for the program to start from. Also rename the UserStorage.txt each time you use the program or it will be overwriten.

/* By Kelechi Nze 
	My first prime project*/
// Section 1 : Precompiled Headers
#include <windows.h> // Uses the Windows header includes alot of windows commands
#include <iostream> // Uses the standard Input and Output commands
#include <fstream>	// File stream
#include <string>	// Allows string variable
// Section 2 : Pre-Main Code
using namespace std; // Uses the namespace std
// Section 3 : Main Code
int main()
{
// Section 3.1 : Asking the user if they want to run the program
	const string NEWLINE = "\n"; // Sets NEWLINE constant to "\n"
	system("TITLE Kelechi Nze's Prime Number Finder v1.8"); // Sets the Title
	cout << "Hello this is my prime number searcher\nversion 1.8 \n"; // Squirts the information to the screen
	cout <<	"Would you like to start finding primes ? (Y \ N)\n"; // Squirts the information to the screen
	string firstdes; // First Desision String Variable Declaration
	cin >> firstdes; // User input into firstdes
	if (firstdes == "N" || firstdes == "n") /* If firstdes is equal to a variation of "n" runs
	the next set of instructions*/
	{ // Begins instructions
		return 0; // Ends program
	} // Ends instructions
// Section 3.2 : The Prime Structure
	struct primes //My Prime number structure
	{
		ifstream startnumb; //The starting number file
		ofstream passnumb; //Passnumb: where primes are stored
		int number, factor, divider;	/*Number: The actulal prime number "finder" 
											factor: What's a factor
											Divider: What to divide the prime.number with to find if there is a prime there*/
	}prime;	//Only one object in the structure ? Yes !
// Section 3.3 : The first few instructions before the Prime number loop
	prime.passnumb.open("UserStorage.txt"); // Opens UserStorage.txt OR creates a new one
	prime.startnumb.open("startnumber.ini");// Opens startnumber.ini
	prime.startnumb >> prime.number; // Puts the information from startnumb.ini and places it in prime.number
	prime.startnumb.close(); // Closes startnumber.ini to make it's resorces available again
	prime.divider = 1; // Sets prime.divider to 1
	prime.factor = 2; // Sets prime.factor to 2
// Section 3.4 : The prime loop
primeloop: //The everlasting loop to get primes
	if (prime.factor > 2) // If prime.factor is bigger than 2 run the next set of instructions
	{ // Begins instructions
		prime.factor = 2; // Sets prime.factor to 2
		prime.number ++; // Increses prime.number by 1
		prime.divider = 1; // Sets prime.divider to 2
		goto primeloop; // The program goes to the primeloop: lable
	} // Ends instructions
	prime.divider ++; // Increses prime.divider by 1
	if ((prime.number % prime.divider) == 0) // If prime.number has remainder 0 when divided by prime.divider run the next set of instructions
	{ // Begins instructions
		prime.factor ++; // Increses prime.factor by 1
	} // Ends instructions
	if (prime.factor == 2 && prime.divider == (prime.number / 2)) // If prime.factor is equal to 2 and prime.divider is equal to prime.number divided by 2 run the next set of instructions
	{ // Begins instructions
		cout << prime.number << " is prime \n"; // Squirts the information to the screen
		prime.passnumb << prime.number << " is prime\n"; // Squirts the information to the screen
	} // Ends instructions
	if (prime.divider == (prime.number / 2)) // If prime.divider is equal to prime.number divided by 2 run the next set of instructions
	{ // Begins instructions
		prime.factor = 2; // Sets prime.factor to 2
		prime.number ++; // Increses prime.number by 1
		prime.divider = 1; // Sets prime.divider to 1
	} // Ends instructions
	goto primeloop; // Goes to the primeloop: label
	prime.passnumb.close(); // Closes prime.passnumb file
}
// Section 4 : Post-Main Code
William Hemsworth 1,339 Posting Virtuoso

It's a bit over-commented.

I mean, a line like... prime.divider ++; // Increses prime.divider by 1 Should be fairly obvious to even a beginner programmer, this just makes your code harder to read.

{ // Begins instructions
} // Ends instructions

Same applies here :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.