| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | Rust | 230917T012536Z | 138 Aspe |
| 075 | JavaScript ES12 | 230916T093010Z | Arnauld |
| 098 | Charcoal | 230915T191518Z | Neil |
| 100 | Ruby | 230915T200911Z | Value In |
Rust, 161 159 bytes
Adapted from the Python code provided on A002088.
Saved 2 bytes thanks to @ceilingcat
Golfed version. Try it online!(TIO has no cached crate, you need to run it locally.)
use cached::proc_macro::cached;
#[cached]
fn f(n:i32)->i32{let(mut c,mut j,mut v)=(0,2,n/2);while v>1{let b=n/v+1;c+=(b-j)*(2*f(v)-1);j=b;v=n/b;}(n*n-n-c+j)/2}
Ungolfed version. Try it online!
use std::collections::HashMap;
struct A002088 {
cache: HashMap<i32, i32>,
}
impl A002088 {
fn new() -> Self {
let mut cache = HashMap::new();
cache.insert(0, 0);
A002088 { cache }
}
fn get(&mut self, n: i32) -> i32 {
match self.cache.get(&n) {
Some(&result) => result,
_ => {
let mut c = 0;
let mut j = 2;
let mut k1 = n / j;
while k1 > 1 {
let j2 = n / k1 + 1;
c += (j2 - j) * (2*self.get(k1) - 1);
j = j2;
k1 = n / j2;
}
let result = (n * (n - 1) - c + j) / 2;
self.cache.insert(n, result);
result
}
}
}
}
fn main() {
let mut a002088 = A002088::new();
let result = a002088.get(10000);
println!("{}", result);
}
JavaScript (ES12), 75 bytes
Adapted from the Python code provided on OEIS.
f=(n,j=2,K)=>f[n]||=n>j?f(K=n/j|0)*(j+=J=~(n/K))-j/2+f(n,-J):n&&(n*~-n+j)/2
Statistics
Assuming the cache is cleared after each call to f:
| \$n\$ | total number of calls |
|---|---|
| 10 | 15 |
| 100 | 193 |
| 1000 | 1407 |
| 10000 | 9111 |
| 100000 | 55009 |
Charcoal, 121 99 98 bytes
⊞υN≔⦃⦄ηFυF¬§ηι«≔ιζ≔²ε≔÷ιεδ≔⟦⟧κW›δ¹«≔⊕÷ιδλ¿§ηδ≧⁺×⁻λε⊖⊗§ηδζ⊞κδ≔λε≔÷ιλδ»¿κF⊞Oκι⊞υλ§≔ηι÷⁺⁻×ιιζε²»I§η⊟υ
Attempt This Online! Link is to verbose version of code. Explanation: Ports the sublinear Python solution mentioned by @CommandMaster.
⊞υN
Input n.
≔⦃⦄η
Set up a dictionary to cache calculated results.
Fυ
Loop over the values that need to be calculated.
F¬§ηι«
Verify that this value hasn't actually been calculated.
≔ιζ≔²ε≔÷ιεδ≔⟦⟧κW›δ¹«≔⊕÷ιδλ¿§ηδ≧⁺×⁻λε⊖⊗§ηδζ⊞κδ≔λε≔÷ιλδ»
Try to calculate the result for this value, but if any dependent values haven't yet been calculated, collect them in a list.
¿κF⊞Oκι⊞υλ
If there were any dependent values that haven't yet been calculated then add them for processing and add the current value for reprocessing.
§≔ηι÷⁺⁻×ιιζε²
Otherwise actually calculate the result for the current value.
»I§η⊟υ
Output the result for the initial value, which is always the last value to be calculated.
Edit: Saved 1 byte thanks to @JonathanAllan.
Ruby, 104 100 bytes
Ports the algorithm at https://oeis.org/A002088. -4 bytes from Jonathan Allan.
A=->n,r=({0=>0}){r[c=n]||(k=n/i=2
(j=n/k+1;c+=(j-i).*2*A[k,r]-1;k=n/i=j)while k>1
r[n]=(n*n-c+i)/2)}