| Bytes | Lang | Time | Link |
|---|---|---|---|
| 026 | Pip | 210630T205048Z | DLosc |
| 075 | C GCC | 230205T130345Z | Peter |
| 058 | R | 210629T083833Z | Dominic |
| 019 | 05AB1E nolazy | 210629T085923Z | ovs |
| 108 | Zephyr | 210630T185259Z | DLosc |
| 029 | Japt v2.0a0 | 210630T091603Z | Shaggy |
| 013 | Husk | 210629T060856Z | Razetime |
| 073 | Haskell | 210629T161905Z | Kyuuhach |
| 051 | Charcoal | 210629T122902Z | Neil |
| 042 | Perl 5 | 210629T090000Z | Kjetil S |
| 047 | Ly | 210629T080459Z | cnamejj |
| 054 | Python 3.8 | 210629T060224Z | dingledo |
| 052 | JavaScript with alert | 210629T062923Z | tsh |
| 034 | Wolfram Language Mathematica | 210629T055035Z | att |
| 078 | Python 3 | 210629T052213Z | hyperneu |
| 019 | Stax | 210629T055743Z | Razetime |
| 045 | Raku | 210629T053237Z | Jo King |
Pip, 26 bytes
Wt*:oYt*o>y*Uo?t+y*Poy*o-t
Outputs infinitely. Attempt This Online!
Explanation
Pip doesn't have rational numbers, so we'll use integer math instead. We store the numerator of the running sum in y (initially "", which evaluates to 0 in a numeric context); the denominator in t (initially 10, but any denominator greater than 0 will do, since the numerator is initially 0); and the index in o (initially 1). Each time through the loop, we want to:
- Increment \$o\$
- Test whether \$\frac y t + \frac 1 o \lt 1\$
- If so, output \$o\$ and add \$\frac 1 o\$ to \$\frac y t\$
- If not, subtract \$\frac 1 o\$ from \$\frac y t\$
For the test, observe that
$$ \frac y t + \frac 1 o \lt 1 \\ \frac{y \cdot o + t}{t \cdot o} \lt 1 \\ y \cdot o + t \lt t \cdot o \\ y \cdot o \lt t \cdot (o - 1) $$
We can combine this expression with the increment of \$o\$ by calculating \$t \cdot o\$ first, then incrementing \$o\$, then calculating \$y \cdot o\$.
For the update, we need
$$ y := y \cdot o \pm t \\ t := t \cdot o $$
Observing that t*:o is always truthy, does not depend on the value of y, and is a no-op if executed before the first time through the loop, we can use it as the while-loop header.
Wt*:oYt*o>y*Uo?t+y*Poy*o-t
t is 10, o is 1, y is "" (implicit)
t*:o Multiply t by o and assign the result back to t
W and loop while the result is truthy (non-zero):
t*o> Is t*o greater than
Uo o, incremented
y* times y?
? If so:
Po Print o
t+y* and calculate t+y*o
Else:
y*o-t Calculate y*o-t
Y Set y to the calculated value
C (GCC), 75 bytes
n,d,i,j;s(a){for(n=i=1,d=j=2;a/i;n/d?n-=2*d/j:++i)n=n*++j+d,d*=j;return j;}
Overflows when the input is greater than 8.
How it works:
// n is the numerator, d the denominator, i the amount of numbers that have been added and j the denominator of the fraction that is supposed to be added.
n,d,i,j;
s(a)
{
// The update statement of the for loop is moved to the end of the loop body for clearness (it's executed at the end of the loop anyways).
// a / i is equivalent to a >= i, so we loop until i is greater than a
for(n = i = 1, d = j = 2; a / i;)
// Both n and d are multiplied by j, and the previous value of d is added to n.
// The new fraction is (n*j + d) / (d*j).
// (n*j)/(d*j) = n/d, so what's actually added to the fraction is d/(d*j), which can be rewritten as 1/j.
n = n * ++j + d,
d *= j,
// n / d is non-zero iff n >= d, and n >= d iff n/d >= 1
// If n/d >= 1, subtract what was previously added to n, twice.
// Otherwise, increment i
n / d ? n -= 2 * d / j : ++i;
return j;
}
This 70-byte version also seems to work, although I'm not sure if it always does:
n,d,i,j;s(a){for(n=i=1,d=j=2;a/i;n/d?n-=2*d/j:++i)n=n*++j+d,d*=j;a=j;}
The 70-byte version stores j in a, which seems to have the same effect as returning j. If anyone knows, please tell me if that behavior is consistent and I'm allowed to use it.
Here's a 127-byte version that overflows at a number of terms somewhere between 21 and 57:
long long n,d,i,j,k;s(a){for(n=i=1,d=j=2;a/i;n/d?n-=2*d/j:++i){n=n*++j+d,d*=j;for(k=1;k<99;)n%++k||d%k||(n/=k,d/=k);}return j;}
It's pretty much the same as the first one, except the variables are long long integers rather than integers, and it includes for(k=1;k<99;)n%++k||d%k||(n/=k,d/=k);, which, for each number k from 1 to 98, divides n and d by k if they're both divisible by k.
R, 59 58 bytes
Edit: -1 byte thanks to emanresu A
c=1
repeat`if`((F=F*(c=c+1)+T)>(T=T*c),F<-F-2*T/c,show(c))
Prints the sequence until it reaches the TIO output limit.
The numerator & denominator of the fraction so far are stored in F and T respectively. These won't error when they get too large, but R will assign them as Inf, beyond which point every integer will be (incorrectly) output, since Inf>Inf is evaluated as FALSE.
A non-overflowing version, using R+GMP to handle large integers, is 93 bytes (the R version installed on TIO seems to give an error, so here is a link to a working version on rdrr.io, with repeat exchanged for while(c<100) to force output).
05AB1E --no-lazy, 20 19 bytes
-1 byte thanks to Kevin Cruijssen!
λN*N<!©+DN!‹iN,ë®·-
There is no builtin fraction type, so everything is integers. (Ab)uses the recursive environment λ as an infinite loop that starts the iteration counter N at 1 and pushes a 1 to the stack initially.
λ # recursive environment
# generate infinite sequence of sums starting with a(0)=1
# pushes the last value to the stack, lets call this x
N* # push N*x
N<!© # calculate (N-1)! and store a copy of the result in the register
+D # calculate N*x + (N-1)! and make a copy of the result
N!‹ # is N*x + (N-1)! < N! ?
iN, # if so, N*x + (N-1)! / N! < 1 and print N
ë®·- # otherwise subtract (N-1)!*2 to get N*x - (N-1)!
Zephyr, 108 bytes
set s to 0
set i to 2
while 1=1
if(s+(/i))<1
set s to(/i)+s
print i
else
set s to s-(/i)
end if
inc i
repeat
Outputs infinitely. Try it online!
Implements the spec directly, using Zephyr's built-in rational numbers. Here it is ungolfed:
set sum to 0
set i to 2
while true
if (sum + (/i)) < 1
set sum to sum + (/i)
print i
else
set sum to sum - (/i)
end if
inc i
repeat
Husk, 13 bytes
W<Goḟε§e+≠1İ\
An infinite list.
woo, it works.
-2 bytes from ovs.
-4 bytes from Dominic Van Essen.
Explanation
Wo<0-Goḟε§e+`-1İ\
1İ\ [1/2,1/3,1/4...
Go 1 scan with 1 as intial value
§e+`- [a+b,a-b]
ḟε first element <=1
Wo indices of pairs where
- difference is
<0 < 0 (negative)
Haskell, 74 73 bytes
p=2#1$0
(n#y)x=(n?y)(x*n)$(n+1)#(y*n)
(n?y)a f|a+y>y*n=f$a-y|1>0=n:f(a+y)
Charcoal, 51 bytes
Nθ≔¹η≔⁰ζ≔¹εW‹Lυθ«≦⊕ε≧×εζ¿›ζ×η⊖ε≧⁻ηζ«≧⁺ηζ⊞υε»≧×εη»Iυ
Try it online! Link is to verbose version of code. Outputs the first n terms. Explanation:
Nθ
Input n.
≔¹η≔⁰ζ≔¹ε
Start with a running total of 0/1 and the last fraction as 1/1.
W‹Lυθ«
Repeat until enough terms have been collected.
≦⊕ε
Increment the denominator to get the next unit fraction.
≧×εζ
Multiply the running total by the denominator.
¿›ζ×η⊖ε≧⁻ηζ
If incrementing the running total would make it exceed the denominator then decrement it.
«≧⁺ηζ⊞υε»
Otherwise increment it and push the denominator to the list of results.
≧×εη
Divide the running total by the denominator.
»Iυ
Print the found terms.
Perl 5, 42 bytes
This prints TSIJMU infinitely:
$i=1;1while$s+=1/++$i*($s+1/$i<1?say$i:-1)
...or until Try It Online reaches its limit of 128 KiB of output. Exploits that say$i prints that sequence number and then returns 1.
This is seven bytes longer and takes n from stdin:
$i=1;$s+=1/++$i*($s+1/$i<1?$_--&&say$i:-1)while$_
Ly, 47 bytes
02s>n[<1f/+1G:![p:lu' o>,<]p[p1l/2*-0]pl`s>]>
02s>n[<1f/+1G:![p:lu' o>,<]p[p1l/2*-0]pl`s>]>
02 # Init stack w/ "0" (sum) "2" (divisor)
s> # Stash the divisor, change to iterator stack
n # Read number of output items "N" from STDIN
[ >,< >] # Loop, iterates until "N" is 0
< # Switch to accumulator stack
1f/+ # Calculate "(1/divisor)+accumulator"
1G: # Compare "accumulator>=1", duplicate answer
![p: ]p # If-then, runs if "accumulator<1"
lu' o # Load the divisor, print as num, add " "
>,< # Switch to iterator stack, decr, switch back
[p o]p # If-then, runs if "accumulator>1"
1l/2*- # Calc "2*(1/divisor)" and subtract
l`s # Increment divisor, stash it
> # Switch to empty stack to suppress printing
Python 3.8, 54 bytes
-1 byte thanks to @ovs
-4 bytes thanks to @att
Outputs the sequence indefinitely.
a=b=n=2
while a:=a*n+[b,-b][a*n>b!=print(n)]:b*=n;n+=1
Although the code is heavily golfed, the idea is a straightforward implementation. a and b are the numerator and denominator of the current fraction. To add two fractions, we can use a simple formula: a/b + c/d => (ad + cb) / bd.
JavaScript (with alert), 52 bytes
for(p=q=i=1n;;p*=i++)q*=i,p*i-p>q?alert(i,q+=p):q-=p
Wolfram Language (Mathematica), 35 34 bytes
#0[#+1/If[++i#>1,-Echo@i,i]]&[i=1]
Outputs the sequence indefinitely (up to $IterationLimit, 4096 by default).
Subtracts terms from 1 instead of adding from 0.
Without overriding $IterationLimit, 40 bytes:
i=0;Do[i+=1/If[i>1/j,-Echo@j,j],{j,∞}]
Python 3, 78 bytes
from fractions import*
v=i=Fraction(1)
while 1:i+=1;v-=(v>1/i!=print(i)or-1)/i
-8 bytes thanks to ovs
-1 byte thanks to Jo King (the number can't be exactly 1, also thanks to Bubbler for the pseudo-proof of this)
-2 bytes thanks to att
Stax, 19 bytes
«efy}á╤2╧8ßÇæ→╔y¬µ!
Making a husk answer for this turned out to be very difficult. Here's a stack based one for the moment.
Raku, 45 bytes
2.FatRat...{($!+=1/$_)>1??($!-=2/$_)!!.say}&1
Full program that outputs the sequence infinitely.