What a silly little project
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
# Devenv
|
||||
.devenv*
|
||||
devenv.local.nix
|
||||
devenv.local.yaml
|
||||
|
||||
# direnv
|
||||
.direnv
|
||||
|
||||
# pre-commit
|
||||
.pre-commit-config.yaml
|
||||
|
||||
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
.envrc
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "compile-time-nth-prime"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "compile-time-nth-prime"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
{
|
||||
"nodes": {
|
||||
"devenv": {
|
||||
"locked": {
|
||||
"dir": "src/modules",
|
||||
"lastModified": 1777989417,
|
||||
"narHash": "sha256-/wC0i5Wls2z66d7G8V99Vs/Mh1TZwaFotmRvY6Ygj1w=",
|
||||
"owner": "cachix",
|
||||
"repo": "devenv",
|
||||
"rev": "02242113054fa55a4f1973a2d51bd996ecb8a242",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"dir": "src/modules",
|
||||
"owner": "cachix",
|
||||
"repo": "devenv",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"inputs": {
|
||||
"nixpkgs-src": "nixpkgs-src"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1776852779,
|
||||
"narHash": "sha256-WwO/ITisCXwyiRgtktZgv3iGhAGO+IB5Av4kKCwezR0=",
|
||||
"owner": "cachix",
|
||||
"repo": "devenv-nixpkgs",
|
||||
"rev": "ec3063523dcd911aeadb50faa589f237cdab5853",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "cachix",
|
||||
"ref": "rolling",
|
||||
"repo": "devenv-nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs-src": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1776329215,
|
||||
"narHash": "sha256-a8BYi3mzoJ/AcJP8UldOx8emoPRLeWqALZWu4ZvjPXw=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "b86751bc4085f48661017fa226dee99fab6c651b",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"devenv": "devenv",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
{ pkgs, ... }:
|
||||
|
||||
{
|
||||
# https://devenv.sh/packages/
|
||||
packages = with pkgs; [ git ];
|
||||
|
||||
# https://devenv.sh/languages/
|
||||
languages.rust.enable = true;
|
||||
|
||||
# See full reference at https://devenv.sh/reference/options/
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
# yaml-language-server: $schema=https://devenv.sh/devenv.schema.json
|
||||
inputs:
|
||||
nixpkgs:
|
||||
url: github:cachix/devenv-nixpkgs/rolling
|
||||
|
||||
# If you're using non-OSS software, you can set allowUnfree to true.
|
||||
# allowUnfree: true
|
||||
|
||||
# If you're willing to use a package that's vulnerable
|
||||
# permittedInsecurePackages:
|
||||
# - "openssl-1.1.1w"
|
||||
|
||||
# If you have more than one devenv you can merge them
|
||||
#imports:
|
||||
# - ./backend
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
use std::f64::consts::{LN_2, SQRT_2};
|
||||
|
||||
const fn base_2_reduction(n: f64) -> (f64, i32) {
|
||||
if n <= 0.0 {
|
||||
return (n, 0);
|
||||
}
|
||||
|
||||
let mut r = n;
|
||||
let mut exp = 0i32;
|
||||
|
||||
while r > SQRT_2 {
|
||||
r /= 2.0;
|
||||
exp += 1;
|
||||
}
|
||||
while r < SQRT_2 / 2.0 {
|
||||
r *= 2.0;
|
||||
exp -= 1;
|
||||
}
|
||||
|
||||
(r, exp)
|
||||
}
|
||||
|
||||
const fn ln_taylor(z: f64) -> f64 {
|
||||
let x1 = z - 1.0;
|
||||
let x2 = x1 * x1;
|
||||
let x3 = x2 * x1;
|
||||
let x4 = x2 * x2;
|
||||
let x5 = x3 * x2;
|
||||
let x6 = x3 * x3;
|
||||
let x7 = x4 * x3;
|
||||
let x8 = x4 * x4;
|
||||
|
||||
x1 - x2 / 2.0 + x3 / 3.0 - x4 / 4.0 + x5 / 5.0 - x6 / 6.0 + x7 / 7.0 - x8 / 8.0
|
||||
}
|
||||
|
||||
const fn ln(n: f64) -> f64 {
|
||||
let (r, e) = base_2_reduction(n);
|
||||
ln_taylor(r) + (e as f64) * LN_2
|
||||
}
|
||||
|
||||
const fn prime_limit(n: usize) -> usize {
|
||||
if n >= 6 {
|
||||
let ln_n = ln(n as f64);
|
||||
(n as f64 * (ln_n + ln(ln_n))).ceil() as usize
|
||||
} else {
|
||||
15
|
||||
}
|
||||
}
|
||||
|
||||
const fn sieve_nth_prime<const N: usize>(n: usize) -> Option<usize> {
|
||||
if n < 1 {
|
||||
return None;
|
||||
}
|
||||
if n == 1 {
|
||||
return Some(2);
|
||||
}
|
||||
|
||||
let limit = prime_limit(n);
|
||||
let mut sieve = [true; N];
|
||||
let mut primes = [0usize; N];
|
||||
let mut prime_index: usize = 0;
|
||||
|
||||
let mut p: usize = 2;
|
||||
while p <= limit {
|
||||
if sieve[p] {
|
||||
primes[prime_index] = p;
|
||||
prime_index += 1;
|
||||
if prime_index + 1 == n {
|
||||
return Some(p);
|
||||
}
|
||||
|
||||
let mut i = p * p;
|
||||
while i <= limit {
|
||||
sieve[i] = false;
|
||||
i += p;
|
||||
}
|
||||
}
|
||||
|
||||
p += 1;
|
||||
}
|
||||
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
macro_rules! nth_prime {
|
||||
($n:expr) => {
|
||||
const {
|
||||
const LIMIT: usize = prime_limit($n + 1) + 1;
|
||||
sieve_nth_prime::<LIMIT>($n + 1).unwrap()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(long_running_const_eval)]
|
||||
const NTH_PRIME: usize = nth_prime!(1000000);
|
||||
|
||||
fn main() {
|
||||
println!("{NTH_PRIME}")
|
||||
}
|
||||
Reference in New Issue
Block a user