Skip to content

Instantly share code, notes, and snippets.

@mgilank
Created March 17, 2024 15:23
Show Gist options
  • Save mgilank/380ed91384ed26c50cd469a0daffd7d7 to your computer and use it in GitHub Desktop.
Save mgilank/380ed91384ed26c50cd469a0daffd7d7 to your computer and use it in GitHub Desktop.
prime.rs
use actix_web::{web, App, HttpResponse, HttpServer};
fn is_prime(n: u64) -> bool {
if n <= 1 {
return false;
}
for i in 2..=(n as f64).sqrt() as u64 {
if n % i == 0 {
return false;
}
}
true
}
async fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello, World!")
}
async fn primes() -> HttpResponse {
let mut prime_numbers = String::new();
for num in 2..=1_000_000 {
if is_prime(num) {
prime_numbers.push_str(&num.to_string());
prime_numbers.push_str("\n");
}
}
HttpResponse::Ok().body(prime_numbers)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/primes", web::get().to(primes))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment