Share sàng ước c++,

hungthinhgaymen

New member
#C ++, #sieve, #Prime số, #AlGorithM, #Programming ## C ++ SEILE OF ERATOSTHENES

Mây của eratosthenes là một thuật toán đơn giản nhưng hiệu quả để tìm tất cả các số nguyên tố lên đến một giới hạn nhất định.Nó được mô tả lần đầu tiên bởi nhà toán học Hy Lạp Eratosthenes vào thế kỷ thứ 3 trước Công nguyên.

Thuật toán hoạt động bằng cách lặp đi lặp lại tất cả các bội số của mỗi số nguyên tố lên đến giới hạn đã cho.Ví dụ: để tìm tất cả các số nguyên tố lên tới 100, chúng tôi sẽ bắt đầu bằng cách đánh dấu tất cả các bội số của 2: 2, 4, 6, 8, 10, ..., 98. Sau đó, chúng tôi sẽ đánh dấu tất cả các bội số của 3:3, 6, 9, 12, ..., 99. Chúng tôi sẽ tiếp tục theo cách này, đánh dấu tất cả các bội số của mỗi số nguyên tố cho đến khi chúng tôi đạt đến giới hạn đã cho.

Các số không được đánh dấu là số nguyên tố.Ví dụ, số nguyên tố lên đến 100 là 2, 3, 5, 7, 11, 13, 17, 19, 23 và 29.

Mây của Eratosthenes là một thuật toán rất hiệu quả.Nó chạy trong thời gian O (n log n), trong đó n là giới hạn đã cho.Điều này nhanh hơn nhiều so với thuật toán vũ phu để tìm số nguyên tố, chạy trong thời gian O (N2).

Mây của Eratosthenes cũng là một thuật toán rất đơn giản để thực hiện.Nó có thể dễ dàng thực hiện trong bất kỳ ngôn ngữ lập trình.

Dưới đây là một ví dụ về cách thực hiện sàng Eratosthenes trong C ++:

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// Hàm này trả về đúng nếu số đã cho là số nguyên tố và sai nếu không.
bool isprime (int n) {
// Số nguyên tố là số tự nhiên lớn hơn 1 không phải là sản phẩm của hai số tự nhiên nhỏ hơn.

// Kiểm tra xem số là 1.
if (n == 1) {
trả lại sai;
}

// Kiểm tra xem số là 2.
if (n == 2) {
trả lại đúng;
}

// Kiểm tra xem số có chia hết cho bất kỳ số nào từ 2 đến căn bậc hai của số không.
for (int i = 2; i <= sqrt (n); i ++) {
if (n % i == 0) {
trả lại sai;
}
}

// Số là số nguyên tố nếu nó không chia hết cho bất kỳ số nào từ 2 đến căn bậc hai của số.
trả lại đúng;
}

// Hàm này in tất cả các số nguyên tố lên đến giới hạn đã cho.
void printprimes (int giới hạn) {
// Tạo một vectơ để lưu trữ các số nguyên tố.
Vector <Int> số nguyên tố;

// lặp qua tất cả các số từ 2 đến giới hạn đã cho.
for (int i = 2; i <= giới hạn; i ++) {
// Kiểm tra xem số là số nguyên tố.
if (isprime (i)) {
// Thêm số vào vectơ của số nguyên tố.
số nguyên tố.push_back (i);
}
}

// In các số nguyên tố.
for (int i = 0; i <primes.size (); i ++) {
cout << số nguyên tố << endl;
}
}

int main () {
// Nhận đầu vào người dùng.
giới hạn int;
cout << "Nhập giới hạn:";
CIN >> giới hạn;

// In các số nguyên tố.
PrintPrimes (giới hạn);

trả lại 0;
}
`` `

## hashtags

* #C ++
* #sieve
* #số nguyên tố
* #AlGorithM
* #Programming
=======================================
#C++, #sieve, #Prime number, #AlGorithM, #Programming ## C++ Sieve of Eratosthenes

The Sieve of Eratosthenes is a simple but efficient algorithm for finding all prime numbers up to a given limit. It was first described by the Greek mathematician Eratosthenes in the 3rd century BC.

The algorithm works by iteratively marking all the multiples of each prime number up to the given limit. For example, to find all the prime numbers up to 100, we would start by marking all the multiples of 2: 2, 4, 6, 8, 10, ..., 98. Then we would mark all the multiples of 3: 3, 6, 9, 12, ..., 99. We would continue in this way, marking all the multiples of each prime number until we reach the given limit.

The numbers that are not marked are the prime numbers. For example, the prime numbers up to 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

The Sieve of Eratosthenes is a very efficient algorithm. It runs in O(n log log n) time, where n is the given limit. This is much faster than the brute-force algorithm for finding prime numbers, which runs in O(n2) time.

The Sieve of Eratosthenes is also a very simple algorithm to implement. It can be easily implemented in any programming language.

Here is an example of how to implement the Sieve of Eratosthenes in C++:

```c++
#include <iostream>
#include <vector>

using namespace std;

// This function returns true if the given number is prime, and false otherwise.
bool isPrime(int n) {
// A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

// Check if the number is 1.
if (n == 1) {
return false;
}

// Check if the number is 2.
if (n == 2) {
return true;
}

// Check if the number is divisible by any number from 2 to the square root of the number.
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}

// The number is prime if it is not divisible by any number from 2 to the square root of the number.
return true;
}

// This function prints all the prime numbers up to the given limit.
void printPrimes(int limit) {
// Create a vector to store the prime numbers.
vector<int> primes;

// Iterate through all the numbers from 2 to the given limit.
for (int i = 2; i <= limit; i++) {
// Check if the number is prime.
if (isPrime(i)) {
// Add the number to the vector of prime numbers.
primes.push_back(i);
}
}

// Print the prime numbers.
for (int i = 0; i < primes.size(); i++) {
cout << primes << endl;
}
}

int main() {
// Get the user input.
int limit;
cout << "Enter the limit: ";
cin >> limit;

// Print the prime numbers.
printPrimes(limit);

return 0;
}
```

## Hashtags

* #C++
* #sieve
* #Prime number
* #AlGorithM
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top