Last active
April 3, 2017 04:14
-
-
Save id4ehsan/132c8ec7c09ca426aea5c35de120cd2e to your computer and use it in GitHub Desktop.
Find prime numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// Prime | |
// | |
// Created by Ehsan Ghasemlou on 8/25/16. | |
// Copyright © 2016 Ehsan Ghasemlou. All rights reserved. | |
// | |
// XMMMWMMM@MMMMMMMMMi | |
// 7MMMMMMMMMMMMMMMMMMW, | |
// MMMMMMMMMM0W@@80MMMMMMi | |
// SMM@Xrrri:, ,rWMMM7 | |
// .MM2. ,8MMB | |
// XMW . i@MM: | |
// SM2.. iBMM: | |
// rM0. . :MMM. | |
// MM:rXr;:. .;SZZZaS; XMM | |
// XW700MMMM2rXZMBMB22X..M7,. | |
// :77.r2r@7Sr :7;2Xr7, ai,; | |
// rX; .,.. : . ... .;.i | |
// ,;i. ., , ..:,i, | |
// ;;:... :i ::, . ..,i:,. | |
// i;;:. . ;S77X;, ....,,: | |
// :;.... .:: ,.,.:: | |
// i:.:r7;i:;;77:....:;,S | |
// ,;:,:::ii:,.:,,,,;r @B, | |
// r;:,,,::,,,..,iSr :MWM2: | |
// 87;,,. .:XWX BB08B@M8X: | |
// :M7rrrr;;;;rXa0r 0W8ZZZZBMMMM0X, | |
// .XW ra8:;rXXSS2XXr;: ZW8ZZZZZBWB0WWM@ | |
// .78MMM7 ;7MX,iiiiiii;,iS SM8ZZZ8880W000088 | |
// ,rZMMMMM@M7..:ZWii;i;i;;i rr;@088Z880ZBB0Z088Z | |
// XMMMMMWWWWW@MM:,;SX;i;rr;r,,.r@0Z08008888B8008ZZ | |
//.aMM@WWWWW@WWWMMrr: iiii::,;, aBZ88008Z0Z80008ZZZ | |
#include <iostream> | |
#include <vector> | |
#include <cstdio> | |
#include <cmath> | |
//the following are UBUNTU/LINUX ONLY terminal color codes. | |
#define RESET "\033[0m" | |
#define BLACK "\033[30m" /* Black */ | |
#define RED "\033[31m" /* Red */ | |
#define GREEN "\033[32m" /* Green */ | |
#define YELLOW "\033[33m" /* Yellow */ | |
#define BLUE "\033[34m" /* Blue */ | |
#define MAGENTA "\033[35m" /* Magenta */ | |
#define CYAN "\033[36m" /* Cyan */ | |
#define WHITE "\033[37m" /* White */ | |
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ | |
#define BOLDRED "\033[1m\033[31m" /* Bold Red */ | |
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ | |
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ | |
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ | |
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ | |
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ | |
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ | |
using namespace std; | |
void find_primes(const unsigned int final){ | |
int k =1; | |
//cout << 1 << " "; | |
cout << RED << 2 << " " << RESET; | |
vector<int> primes; | |
primes.push_back(2); | |
for(int i=3; i < final; i++) | |
//we can change i++ with i+=2 to make it faster because there is only odd prime number | |
{ | |
bool prime=true; | |
for(int j=0;j<primes.size() && primes[j]*primes[j] <= i;j++) | |
{ | |
if(i % primes[j] == 0) | |
{ | |
prime=false; | |
break; | |
} | |
} | |
if(prime) | |
{ | |
primes.push_back(i); | |
cout << RED << i << " " << RESET; | |
//cout << i << " "; | |
}else if(!prime){ | |
cout << i << " "; | |
} | |
k++; | |
if(k==2*3){k=0; cout << endl;} | |
} | |
} | |
int main(int argc, const char * argv[]) { | |
find_primes(1000000); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment