Sum Chain Sequence
R, 85 82 79 76 72 70 bytes
for(i in 2:56)T[i]=((z=i+T[i-1])+i*(!z%%2)-i*(!z%%3))*`if`(z%%4,1,i);T
ungolfed:
s=1 ## formerly s=1:56, formerly s=1:100
for(i in 2:56){
z=i+s[i-1]
s[i]=(z+i*(z%%2<1)-i*(z%%3<1))*(1+(i-1)*(z%%4<1))
}
s
Thanks to @rturnbull for pointing out that I can use (!z%%3)
instead of (z%%3<1)
to check the moduli, and that the definition of z
an happen when it is first used.
Golfed away 3-4 chars by abusing vector extension: the answer originally started s=1:56...
but we don't need to do that, the length of s
will be extended as needed.
Saved 3 more bytes by replacing the last condition with a call to the "if"
function (yes, it's a proper function in R!)
Saved 4 more bytes by replacing s
with T
, which is a builtin equal to TRUE
which is also equal to 1
. I realised it at the same time as @rturnbull (honest!)
This does suffer from some numerical issues once we exceed 2^52, but there is nothing I can do about that --- R can only use double
types for numbers larger than 2^31-1
, but they store integers up to 2^52 exactly. Thus, I am allowed to only output the first 56 terms (the last term which is "right") which saves one byte over the 100-length case.
Here's the output from the 56-length version:
> for(i in 2:56){z=i+T[i-1];T[i]=(z+i*(!z%%2)-i*(!z%%3))*`if`(z%%4,1,i)};T
[1] 1 1 21 25 30 216
[7] 223 223 2169 2179 2190 2202
[13] 2215 2215 2245 2261 2295 2295
[19] 2333 2353 2395 2417 56649 56649
[25] 56699 56725 1533033 1533061 1533090 45993600
[31] 45993631 45993631 1517792001 1517792035 1517792070 1517792106
[37] 1517792143 1517792143 1517792221 1517792261 1517792343 1517792343
[43] 1517792429 1517792473 1517792563 1517792609 71336257041 71336257041
[49] 71336257139 71336257189 3638149121841 3638149121893 3638149121946 196460052588000
[55] 196460052588055 196460052588055
Python 3, 82 78 76 74 72 bytes
i=s=1
exec('print(s);i+=1;s+=i;s=(s+i-i*(s%2+(s%3<1)))*i**(s%4<1);'*100)
Output:
1
1
21
25
30
216
223
223
2169
2179
2190
2202
2215
2215
2245
2261
2295
2295
2333
2353
2395
2417
56649
56649
56699
56725
1533033
1533061
1533090
45993600
45993631
45993631
1517792001
1517792035
1517792070
1517792106
1517792143
1517792143
1517792221
1517792261
1517792343
1517792343
1517792429
1517792473
1517792563
1517792609
71336257041
71336257041
71336257139
71336257189
3638149121841
3638149121893
3638149121946
196460052588000
196460052588055
196460052588055
11198222997525633
11198222997525691
11198222997525750
11198222997525810
11198222997525871
11198222997525871
11198222997525997
11198222997526061
11198222997526191
11198222997526191
11198222997526325
11198222997526393
11198222997526531
11198222997526601
795073832824398753
795073832824398753
795073832824398899
795073832824398973
59630537461829934225
59630537461829934301
59630537461829934378
4651181922022734887568
4651181922022734887647
4651181922022734887647
376745735683841525912529
376745735683841525912611
376745735683841525912694
376745735683841525912778
376745735683841525912863
376745735683841525912863
376745735683841525913037
376745735683841525913125
376745735683841525913303
376745735683841525913303
376745735683841525913485
376745735683841525913577
376745735683841525913763
376745735683841525913857
35790844889964944961834465
35790844889964944961834465
35790844889964944961834659
35790844889964944961834757
3543293644106529551221660545
3543293644106529551221660645
Suggestions are welcome!
05AB1E, 34 31 30 bytes
XTnFD,NÌ©+D3L>%_`X®‚sèrŠs-®*+*
Try it online!
Explanation
X # initialize stack with 1
TnF # for N in [0 ... 99]
D, # print a copy of top of stack
NÌ© # increase index N by 2 and store in register
+ # add this to current value
D # make a copy of the current value
3L> # push the list [2,3,4]
% # take current value mod elements in list
_ # invert this
` # push the elements from the list to stack
X®‚sè # index into list [1,N+2] with the result of mod 4
rŠs- # subtract result of mod 3 from result of mod 2
®* # multiply by N+2
+ # add this to current value
* # multiply current value with the result from index operation