Armenian Knowledge Base  

Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters > Algorithms
Register

Reply
 
LinkBack Thread Tools
Old 25.01.2014, 21:19   #1
The splendid
 
AvDav's Avatar
 
Join Date: 07 2004
Location: Universe
Age: 36
Posts: 3,413
Downloads: 22
Uploads: 0
Reputation: 222 | 3
Default Sieve of Eratosthenes (Algorithm of generating primes)

Еще одна задача из той же категории и её компактная реализация:
Code:
#include <vector>
#include <iostream>

using namespace std;

void generate_primes(int n) {
	vector<bool> v(n+1, true);
	v[0] = v[1] = false;
	for(int i = 2; i*i <= n; ++i)
		if(v[i]) for(int j = (i << 1); j <= n; j+=i) v[j] = false;
	for(int i = 0; i <= n; ++i)
		if(v[i]) cout << i << endl;
}

int main() {
	generate_primes(100);
	return 0;
}
Reply With Quote
Reply

Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Doomsday Algorithm Hrach_Techie Algorithms 4 21.10.2004 17:37
Generating Random values from JavaScript x.ice Web Development 2 17.02.2004 14:03
Booth's Multiplication Algorithm DaNYer Algorithms 9 24.11.2003 05:47
Array shuffleing algorithm strax. Algorithms 5 16.06.2002 19:43
Bit packing best algorithm greka Algorithms 27 24.05.2002 14:01


На правах рекламы:
реклама

All times are GMT. The time now is 14:02.


Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.