I'm fiddling with Rust, using prime numbers as a playground.
A Fermi–Dirac prime is a prime power whose exponent is a power of two. Interestingly, this concept already confuses chatbots quite a bit.
I came up with the following solution which should be readable enough, can somebody tell me if there is something wrong with it?
https://oeis.org/A050376
=> More informations about this toot | More toots from adbar@fediscience.org
@HydrePrever et al. your 2¢ maybe? Interesting series related to primes?
=> More informations about this toot | More toots from adbar@fediscience.org
@adbar I am not sure I'm getting the algorithm actually. You first test if n is prime, but n = 4 is a Fermi-Dirac prime ; if I read correctly your function returns false in that case.
I think when n is not prime, you should look at its square root m. If m is not an integer, the case is dealt with. If m is an integer, then n is FD iff m is FD.
=> More informations about this toot | More toots from HydrePrever@mathstodon.xyz
@adbar I don't know enough rust (but I need to learn it), I think like in c you need to do your function with modulo (%) and not with division (/). Maybe I'm wrong.
=> More informations about this toot | More toots from nours@mastouille.fr
@adbar fn is_prime(n: u64) -> bool {
if n <= 1 {
return false;
}
if n <= 3 {
return true;
}
if n % 2 == 0 || n % 3 == 0 {
return false;
}
let sqrt_n = (n as f64).sqrt() as u64;
let mut i = 5;
while i <= sqrt_n {
if n % i == 0 || n % (i + 2) == 0 {
return false;
}
i += 6;
}
true
}
=> More informations about this toot | More toots from nours@mastouille.fr
@adbar fn find_base_and_exp(n: u64) -> Option<(u64, u64)> {
// Si n est 1, ce n'est pas un primaire de Fermi-Dirac
if n == 1 {
return None;
}
// Pour chaque nombre premier possible comme base
let sqrt_n = (n as f64).sqrt() as u64;
let mut p = 2;
while p <= sqrt_n + 1 {
if !is_prime(p) {
p += 1;
continue;
}
let mut x = n;
let mut exp = 0;
=> More informations about this toot | More toots from nours@mastouille.fr
@adbar // Tant que x est une puissance parfaite de p
while x > 1 && x % p == 0 {
x /= p;
exp += 1;
}
// Si x = 1, on a trouvé notre décomposition
if x == 1 {
return Some((p, exp));
}
p += 1;
}
// Si n est lui-même premier, c'est un cas spécial (exposant 1)
if is_prime(n) {
return Some((n, 1));
}
None
}
=> More informations about this toot | More toots from nours@mastouille.fr
@adbar fn is_power_of_two(n: u64) -> bool {
n != 0 && (n & (n - 1)) == 0
}
fn is_fermi_dirac_prime(n: u64) -> bool {
if let Some((base, exp)) = find_base_and_exp(n) {
// Vérifie si l'exposant est une puissance de 2
is_power_of_two(exp)
} else {
false
}
}
=> More informations about this toot | More toots from nours@mastouille.fr
@nours Merci ! Le code pour les puissances de 2 est pratique, pour le reste je préfère passer par des bool plutôt que des Option.
J'ai le même type de calcul des nombres premiers, pas le plus rapide mais plus facile à écrire.
Je publierai le code en open source bientôt, on pourra en reparler.
=> More informations about this toot | More toots from adbar@fediscience.org
@adbar avec plaisir
=> More informations about this toot | More toots from nours@mastouille.fr This content has been proxied by September (3851b).Proxy Information
text/gemini