Skip to main content

corevm_prime_sieve/
lib.rs

1#![cfg_attr(any(target_arch = "riscv32", target_arch = "riscv64"), no_std)]
2
3use corevm_guest::println;
4
5#[polkavm_derive::polkavm_export]
6pub extern "C" fn main() -> u64 {
7	// This is probably the most inefficient way of generating primes.
8	// Works perfectly as a test though.
9	for i in 0..u64::MAX {
10		if is_prime_naive(i) {
11			println!("{}", i);
12		}
13	}
14	0
15}
16
17fn is_prime_naive(n: u64) -> bool {
18	if n <= 1 {
19		return false;
20	}
21	if n == 2 {
22		return true;
23	}
24	// This should normally be `sqrt(n)`.
25	let sqrt_n = n / 2;
26	for i in 2..=sqrt_n {
27		if n % i == 0 {
28			return false
29		}
30	}
31	true
32}
33
34#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
35#[panic_handler]
36fn panic(info: &core::panic::PanicInfo) -> ! {
37	corevm_guest::panic(info)
38}