Please write a Rust function that takes a u64 input and returns the largest prime number…
- User
Please write a Rust function that takes a u64 input and returns the largest prime number less than that value.
- Assistant
fn largest_prime(n: u64) -> u64 { if n < 2 { return 0; } for i in (2..=(n - 1)).rev() { let mut is_prime = true; for j in 2..=(i as f64).sqrt() as u64 + 1 { if i % j == 0 { is_prime = false; break; } } if is_prime { return i; } } 2 }
This function returns the largest prime number less than the input
n. It does so by checking the primality of every number less thannstarting fromn. There are more efficient ways to compute primality, but here we check whether any number less than the square root of the candidate prime is a factor of that candidate.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)