Very Simple Prime Number Finder v1.8

kelechi96 0 Tallied Votes 258 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

Dani AI

Generated

Nice first project, — a clear learning exercise. Two practical problems to fix: the code uses goto/labels and tests divisors up to n/2 (very slow), and the output file is overwritten each run with no graceful stop. As pointed out, trim line-by-line comments: prefer short, intent-focused comments (what the block does, not what ++ does).

Concrete fixes to apply

  • Replace the goto-based state machine with a simple loop and a small is_prime function that tests divisors only up to sqrt(n). Skip even divisors after handling 2.
  • Use unsigned long long for larger range and detect file errors (missing startnumber.ini) with a sensible default (start at 2).
  • Open the output file in append mode (ios::app) so you don't need to manually rename UserStorage.txt.
  • Add an explicit stop condition (max count, max value, or user interrupt) so the program doesn’t run forever.

Practical example (minimal, modern C++ style):

#include <iostream>
#include <fstream>
#include <cmath>

using ull = unsigned long long;

bool is_prime(ull n) {
    if (n < 2) return false;
    if (n % 2 == 0) return n == 2;
    for (ull d = 3; d * d <= n; d += 2)
        if (n % d == 0) return false;
    return true;
}

Open startnumber.ini (default 2) and write primes with std::ofstream out("UserStorage.txt", std::ios::app);. Loop with for (ull n = start; /*stop condition*/; ++n) and call is_prime(n).

Extra notes: for large ranges use a segmented sieve (much faster when you need many primes). Watch integer overflow (ULL max ≈ 2^64−1) and remember that heavy prime searches can be CPU- and I/O-intensive — add checkpoints or rotate output files if you plan long runs.

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.