#C, trial division, 73 bytes# i=1; main(n){for(scanf("%d",&n);n%++i&&i*i<n;);printf("%d",n<3?n-1:n%i);} Note special handling for n=1,2 We need to stop the loop before i gets to n-1, so `i<n-1` would do, but `i*i<n` is more efficient.