Victory Road  

Go Back   Victory Road > General > Technology

Notices

 
 
Search this Thread
  #1  
Old September 24, 2008, 06:54:10 PM
SpaceMan++'s Avatar
SpaceMan++ SpaceMan++ is offline
Zoroark
 
Join Date: Aug 2008
Location: BC
Posts: 288
Default Prime Number

I made a command line program in C/C++ that outputs a list of prime numbers.
I only compiled on Linux, so there may be some problems on Windows, if there's problem on Windows, create a new Visual Studio project with the following source code:
Code:
//Prime number calculator
//This program lists all prime numbers lower than certain value.
// Created on September 23, 2008, 10:18 PM

#include <stdlib.h>
#include <iostream>

//check for prime number
bool isPrime(int integer)
{
	//loop to mod with any numbers lower than integer-1 and higher than 1
	for (int i = 2; i < integer; i++)
	{
		//skip and return false if 0 was returned.
		if (integer % i == 0)
		{
			return false;
			break;
		}
	}

	//if 0 was not returned, this is a prime number.
	return (true);
}
int main(int argc, char** argv)
{
	//preparations
	std::cout << "Prime Number Generator 1.0\nThis program outputs all prime numbers lower than certain value. If you entered invalid value, the program may be buggy. \nEnter 0 or 1 to exit.";

	//----------
	restart:
	std:: cout << "Enter the max. limit: \n>";
	//long double is the biggest possible data type
	long double limit;

	//input the limit
	std::cin >> limit;
	std::cout << "\n";

	//if 0 and 1 was entered, exit.
	if (limit <= 1)
	{
		std::cout << "Exit.\n";
		return (EXIT_SUCCESS);
	}

	//main loop
	//the counter increase 2 every time for performance.
	for (int current = 3; current < limit; current += 2)
	{
		//keep checking the current value
		//if it is prime, output the value
		if (isPrime(current))
		{
			std::cout << current << "\n";
		}
	}
	goto restart; //go back

	//exit in case of bugs
	return (EXIT_FAILURE);
}
and see attachment for the compiled executable
Now I tried to enter 99999999999999999999999999 and find what's the limit of the program
Attached Files
File Type: zip exec.zip (32.2 KB, 339 views)

Last edited by Cat333Pokémon; December 30, 2009 at 12:16:07 PM.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump


All times are GMT -8.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Victory Road ©2006 - 2024, Scott Cat333Pokémon Cheney
Theme by A'bom and Cat333Pokémon