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

Reply
 
Thread Tools

Sieve of Eratosthenes (Algorithm of generating primes)
Old 25.01.2014, 20:19   #1
Ego coder
 
AvDav's Avatar
 
Join Date: 07 2004
Location: Yerevan, Armenia
Age: 43
Posts: 3,738
Rep Power: 4
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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Doomsday Algorithm Hrach_Techie Algorithms 4 21.10.2004 16: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 18:43
Bit packing best algorithm greka Algorithms 27 24.05.2002 13:01


Реклама:
реклама

All times are GMT. The time now is 12:29.
Top

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