Skip to content

Commit

Permalink
Update consolidated snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jan 8, 2025
1 parent 0856466 commit c8acffc
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions public/consolidated/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@
],
"contributors": [],
"code": "bool is_prime(int n) {\n if (n < 2) return false;\n if (n == 2 || n == 3) return true;\n if (n % 2 == 0) return false;\n for (int i = 3; i * i <= n; i += 2) {\n if (n % i == 0) return false;\n }\n return true;\n}\n\n// Usage:\nis_prime(29); // Returns: true\n"
},
{
"title": "Sieve of Eratosthenes",
"description": "Generate all prime numbers up to a given integer using the Sieve of Eratosthenes algorithm",
"author": "dibyam-jalan27",
"tags": [
"number",
"prime"
],
"contributors": [],
"code": "#include <vector>\n\nusing namespace std;\n\nvector<int> sieve_of_eratosthenes(int n) {\n vector<bool> is_prime(n + 1, true);\n vector<int> primes;\n is_prime[0] = is_prime[1] = false;\n for (int i = 2; i * i <= n; ++i) {\n if (is_prime[i]) {\n for (int j = i * i; j <= n; j += i) {\n is_prime[j] = false;\n }\n }\n }\n for (int i = 2; i <= n; ++i) {\n if (is_prime[i]) {\n primes.push_back(i);\n }\n }\n return primes;\n}\n\n// Usage:\nsieve_of_eratosthenes(30); // Returns: {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}\n"
}
]
},
Expand Down

0 comments on commit c8acffc

Please sign in to comment.