16
\$\begingroup\$

Sequence:

  1. We start at 1.
  2. We first add the current 1-indexed value to the previous number in the sequence.
  3. Then we apply the following mathematical operations in order if they apply to this current value:
    • Divisible by 2? => Addition
    • Divisible by 3? => Subtraction
    • Divisible by 4? => (Addition AND) Multiply
    • Not divisible by neither 2, 3 nor 4? -> Continue with current sum-result

Output:

Output the first 100 numbers in this sequence:

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

Here are the first 10 numbers in the sequence with explanation:

// Starting number of the sequence:
1

// 1 (previous number in the sequence)
// + 2 (current index in 1-indexed sequence)
// = 3 -> 3 - 2 (3 is divisible by 3, so we subtract the current index 2)
// = 1
1

// 1 (previous number in the sequence)
// + 3 (current index in 1-indexed sequence)
// = 4 -> 4 + 3 (4 is divisible by 2, so we first add the current index 3)
// = 7 -> 7 * 3 (and 4 is also divisible by 4, so we then also multiply the current index 3)
// = 21
21

// 21 (previous number in the sequence)
// + 4 (current index in 1-indexed sequence)
// = 25 (25 is not divisible by 2, 3 nor 4)
25

// 25 (previous number in the sequence)
// + 5 (current index in 1-indexed sequence)
// = 30 -> 30 + 5 (30 is divisible by 2, so we first add the current index 5)
// = 35 -> 35 - 5 (and 30 is also divisible by 3, so we then also subtract the current index 5)
// = 30
30

// 30 (previous number in the sequence)
// + 6 (current index in 1-indexed sequence)
// = 36 -> 36 + 6 (36 is divisible by 2, so we first add the current index 6)
// = 42 -> 42 - 6 (and 36 is also divisible by 3, so we then also subtract the current index 6)
// = 36 -> 36 * 6 (and 36 is also divisible by 4, so we then also multiply the current index 6)
// = 216
216

// 216 (previous number in the sequence)
// + 7 (current index in 1-indexed sequence)
// = 223 (223 is not divisible by 2, 3 nor 4)
223

// 223 (previous number in the sequence)
// + 8 (current index in 1-indexed sequence)
// = 231 -> 231 - 8 (231 is divisible by 3, so we subtract the current index 8)
// = 223
223

// 223 (previous number in the sequence)
// + 9 (current index in 1-indexed sequence)
// = 232 -> 232 + 9 (232 is divisible by 2, so we first add the current index 9)
// = 241 -> 241 * 9 (and 232 is also divisible by 4, so we then also multiply the current index 9)
// = 2169
2169

// 2169 (previous number in the sequence)
// + 10 (current index in 1-indexed sequence)
// 2179 (2179 is not divisible by 2, 3 nor 4)
2179

Challenge rules:

  • If your language doesn't support anything bigger than 231-1, you can continue the sequence until that max (so the first 46 numbers, up until - and including - 1,517,792,609).
  • The output format is flexible. You can return an array or list, a string separated with spaces, commas, etc. Your call.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.
\$\endgroup\$
  • \$\begingroup\$ Do we output the nth value, the first n values, or just until our max integer size? \$\endgroup\$ – Gabriel Benamy Oct 25 '16 at 13:28
  • \$\begingroup\$ @GabrielBenamy The first 100 in the sequence. \$\endgroup\$ – Kevin Cruijssen Oct 25 '16 at 13:29
  • 1
    \$\begingroup\$ I'm pretty sure you only have 99 numbers in that block. \$\endgroup\$ – Kade Oct 25 '16 at 13:39
  • 2
    \$\begingroup\$ My answer disagrees with your output on only the last 13 numbers. \$\endgroup\$ – Gabriel Benamy Oct 25 '16 at 13:41
  • 1
    \$\begingroup\$ @Shebang Fixed.. Sorry for the sloppy start.. It has been in the sandbox for 5 days, but I guess neither me nor the others have noticed it.. :S Should be correct now. \$\endgroup\$ – Kevin Cruijssen Oct 25 '16 at 13:50

14 Answers 14

1
\$\begingroup\$

05AB1E, 24 23 bytes

-1 byte thanks to Kevin Crujissen

¼¾тF=¼¾+©…+-*v®NÌÖi¾y.V

Try it online!

Explanation:

¼¾                        # set the counter to 1, then push 1
  тF                      # repeat the following 100 times
    =                     # print the current number in the sequence
     ¼¾                   # increment the counter
       +                  # add it to the current number
        ©                 # save the result in the register
         …+-*v            # for each of '+', '-', and '*'...
              ®   i       # if the register...
                 Ö        # is divisible by...
               NÌ         # the loop index + 2...
                   ¾y.V   # then apply the current operation
\$\endgroup\$
  • 1
    \$\begingroup\$ Tried to find something shorter with the counter variable so the UX can be removed, but I'm unable to. I end up at 24 bytes as well because it starts at 0 instead of 1. I now increased it before the while, but then we have to loop 101 times instead of 100.. Ah well. \$\endgroup\$ – Kevin Cruijssen May 29 at 11:10
  • \$\begingroup\$ @KevinCruijssen yeah, that UX is an eyesore. I tried to get rid of it for a while and ended up with a bunch of 24 and 25 variations: 1тF=NÌ©+DÑ3L>Ãv®…-*+yè.V, 1тL>v=y+©3F®NÌÖiy…+-*Nè.V... I didn't consider using the counter variable, this is interesting. \$\endgroup\$ – Grimmy May 29 at 11:26
  • 1
    \$\begingroup\$ @KevinCruijssen your 24 inspired a 23: just use тF instead of Ƶ0µ. I've edited that in, thanks! (PS: there should really be a single-byte¼¾...) \$\endgroup\$ – Grimmy May 29 at 11:34
  • \$\begingroup\$ Ah nice. Figured you'd find something somehow, haha. ;) And yeah, a single byter for ¼¾ would be nice, although to be quite honest, I almost never use it like that. The single-byte builtin I would prefer most right now is a second ©® variable which doesn't pop. Perhaps starting at an empty string "" as you mentioned in another challenge before. \$\endgroup\$ – Kevin Cruijssen May 29 at 13:02
8
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ I would say looping only up to 56 is fair game given the description of the challenge. \$\endgroup\$ – Billywob Oct 25 '16 at 14:53
  • \$\begingroup\$ @Billywob is indeed right. In the description I state "If your language doesn't support anything bigger than 2^31-1, you can continue the sequence until that max (so the first 46 numbers, up until - and including - 1,517,792,609).", but this of course also applies for different numbers than 32-bit. If R can't handle anything bigger, than the first 56 numbers is completely fine. And yes, if you know it can never go above 56, you may change the 100 to 56 to save a byte. \$\endgroup\$ – Kevin Cruijssen Oct 25 '16 at 15:08
  • 1
    \$\begingroup\$ You can save three bytes by changing z%%2<1 (and so on) to !z%%2, abusing implicit type conversion. \$\endgroup\$ – rturnbull Oct 25 '16 at 15:09
  • \$\begingroup\$ Thanks @rturnbull, for some reason I thought ! didn't beat %%, but apparently it does! \$\endgroup\$ – JDL Oct 25 '16 at 15:11
  • 2
    \$\begingroup\$ You can also abuse T and use that in place of s, allowing you to remove the s=1;, saving another four bytes. It's possible to fold the definition of z into the definition of s[i] (well, T[i] now), like so: T[i]=((z=i+T[i-1])+ ..., which means that you can lose the curly brackets, saving some more bytes. EDIT: Oh, I see that you did the T trick while I was writing my comment! Great minds think alike, they say. \$\endgroup\$ – rturnbull Oct 25 '16 at 15:24
5
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Python 2, 76 Bytes

Pretty standard implementation, I think using an exec statement rather than a while loop saved 2 bytes or so. A recursive method may be shorter, I imagine xnor will pop up soon ;)

n=1
f=1
exec'print f;n+=1;d=f+n;f=(d+n*(d%2<1)-n*(d%3<1))*[1,n][d%4<1];'*100

If I used the updates that TheNumberOne figured out, I would be at 69 bytes (but then I would be copying)

n=f=1;exec'print f;n+=1;d=f+n;f=(d+n-n*(d%2+(d%3<1))*n**(d%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
\$\endgroup\$
3
\$\begingroup\$

JavaScript, 75 63 bytes

for(n=p=0;n++<57;alert(p=p%4?q:q*n))q=(p+=n)%2?p:p+n,q-=p%3?0:n

Another version:

for(n=p=0;n++<57;)alert(p=((p+=n)+(!(p%2)-!(p%3))*n)*(p%4?1:n))

Both stop at index 57 (0-indexed) because that's when the output surpasses JavaScript's safe number size (253 - 1). Turns out a loop is way shorter than a recursive function, even with ES6:

f=(n=0,p=0)=>n++>56?[]:(q=(p+=n)%2?p:p+n,q-=p%3?0:n,[q*=p%4?1:n,...f(n,q)])

This one returns an array of the first 57 elements.

\$\endgroup\$
  • \$\begingroup\$ I think you should avoid going above ~50-60 because then you exceed Number.MAX_SAFE_INTEGER, and your divisions will become incorrect. I also tried the map version for completeness and it too clocked in at 75 bytes. \$\endgroup\$ – Neil Oct 25 '16 at 14:49
  • \$\begingroup\$ @Neil Ah, thanks. To be precise, it surpasses Number.MAX_SAFE_INTEGER after 57 entries. \$\endgroup\$ – ETHproductions Oct 25 '16 at 15:32
3
\$\begingroup\$

Brain-Flak 476 466 462 456 446 Bytes

Saved 6 bytes thanks to Wheat Wizard

(((((((())<>()(())()){}){}){}())){}{}){({}[()]<(((({})<>({}())<>))<({}(()())(<()>)){({}[()]<(({}()[({})])){{}(<({}({}))>)}{}>)}({}{}<{}(())>){((<{}{}>))}{}{(<{}({}<>({})<>)>)}{}>)(({})<({}(()()())(<()>)){({}[()]<(({}()[({})])){{}(<({}({}))>)}{}>)}({}{}<{}(())>){((<{}{}>))}{}{(<{}({}<>[({})]<>)>)}{}>)({}(()()()())(<()>)){({}[()]<(({}()[({})])){{}(<({}({}))>)}{}>)}({}{}<{}(())>){((<{}{}>))}{}{(<{}(<>({}))({<({}[()])><>({})<>}{}<><{}>)>)}{}>)}{}

Try it Online!

This is really slow. TIO can't handle the whole 100 numbers (the limit seems to be 22 or 23). So, this example only generates the first 20, but the code would work for 100 as well.

Brief Explanation:

      (())<>                           # push a 1 (the index) and switch stacks 
            (())                       # then push a 1 (the starting number)
((((((          ()()){}){}){}())){}{}) # and a 99 (a counter so that we only print the 
                                       # first 100 numbers)

# repeat until the counter is 0
{
  # pop the counter and push it minus 1 after:
  ({}[()]<
    # hold onto the current number plus the index (leave a copy on the stack to be printed)
    # and increment the index
    (((({})<>({}())<>))<
      # push logical not of (current mod 2)
      ({}(()())(<()>)){({}[()]<(({}()[({})])){{}(<({}({}))>)}{}>)}({}{}<{}(())>){((<{}{}>))}{}
      # if !(current mod 2) is 1, add the index
      {(<{}({}<>({})<>)>)}{}
    # push the current number back on
    >)
    # hold onto the current number
    (({})<
     # push logical not of (current mod 3)
     ({}(()()())(<()>)){({}[()]<(({}()[({})])){{}(<({}({}))>)}{}>)}({}{}<{}(())>){((<{}{}>))}{}
     # if !(current mod 3) is 1, then subtract the index
     {(<{}({}<>[({})]<>)>)}{}
    # push the current number back on
    >)
    # push logical not of (current mod 4)
    ({}(()()()())(<()>)){({}[()]<(({}()[({})])){{}(<({}({}))>)}{}>)}({}{}<{}(())>){((<{}{}>))}{}
    # if !(current mod 4) is 1, multiply by the index
    {(<{}(<>({}))({<({}[()])><>({})<>}{}<><{}>)>)}{}
  # put the counter back on
  >)
# loop until done
}
# pop the counter
{}
\$\endgroup\$
  • \$\begingroup\$ ({}<>[({})]<>)(<()>) can be replaced with (<({}<>[({})]<>)>) \$\endgroup\$ – Wheat Wizard Oct 25 '16 at 19:49
  • \$\begingroup\$ @WheatWizard Updated. Thanks! \$\endgroup\$ – Riley Oct 25 '16 at 19:54
1
\$\begingroup\$

Java 7, 316 bytes

import java.math.*;String c(){String r="";BigInteger t=BigInteger.ONE,x,p;for(int i=2;i<102;){r+=t+" ";p=(t=t.add(x=new BigInteger(i+++"")));t=x(p,2)?t.add(x):t;t=x(p,3)?t.subtract(x):t;t=x(p,4)?t.multiply(x):t;}return r;}boolean x(BigInteger p,int i){return p.mod(new BigInteger(i+"")).compareTo(BigInteger.ONE)<0;}

Ungolfed & test code:

Try it here.

import java.math.*;
class M{
  static String c(){
    String r = "";
    BigInteger t = BigInteger.ONE,
               x,
               p;
    for(int i = 2; i < 102;){
      r += t+" ";
      p = (t = t.add(x = new BigInteger(i++ + "")));
      t = x(p, 2)
           ? t.add(x)
           : t;
      t = x(p, 3)
           ? t.subtract(x)
           : t;
      t = x(p, 4)
           ? t.multiply(x)
           : t;
    }
    return r;
  }

  public static void main(String[] a){
    System.out.println(c());
  }

  static boolean x(BigInteger p, int i){
    return p.mod(new BigInteger(i+"")).compareTo(BigInteger.ONE) < 0;
  }
}

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 3424140340272 3424140340321 3424140340371 3424140340473 3424140340525 3424140340631 3424140340631 3424140340741 3424140340797 3424140340911 3424140340969 202024280124133 202024280124193 202024280124315 202024280124377 12727529647843689 814561897462000192 52946523335030016705 52946523335030016771 52946523335030016905 52946523335030016973 52946523335030017111 52946523335030017111 52946523335030017253 52946523335030017253 52946523335030017399 52946523335030017473 3970989250127251321725 301795183009671100456876 301795183009671100456953 301795183009671100457031 301795183009671100457110 301795183009671100457270 301795183009671100457351 301795183009671100457433 25049000189802701337980717 25049000189802701337980801 25049000189802701337980971 25049000189802701337981057 2179263016512835016404367097 191775145453129481443584312280 17067987945328523848479003800841 1536118915079567146363110342083790 1536118915079567146363110342083790 1536118915079567146363110342083974 1536118915079567146363110342083974 144395178017479311758132372155911228 13717541911660534617022575354811575685 13717541911660534617022575354811575781 13717541911660534617022575354811575975 13717541911660534617022575354811576073 1358036649254392927085234960126346050829 
\$\endgroup\$
1
\$\begingroup\$

C#, 120 Bytes

Just as no sane person would golf in Java, no sane person should golf in C#! But screw that, I wanted to see what I can do. The 1M casts f to be a decimal which has enough precision for this answer without me having to write decimal. Also, the in-place incrementing saves some bytes on my Python answer. in the end it is still 50 bytes longer.

void k(){int n=1;var f=1M;while(n<101){Console.WriteLine(f);var d=++n+f;f=(d+n*((d%2<1?1:0)-(d%3<1?1:0)))*(d%4<1?n:1);}}

Here's the more readable (and runnable) version:

using System;
class P
{
    static void Main(string[]a) 
    {
        int n = 1;
        var f = 1M;
        while (n < 101) 
        {
            Console.WriteLine(f);
            var d = ++n + f;
            f = (d + n * ((d % 2 < 1 ? 1 : 0) - (d % 3 < 1 ? 1 : 0))) * (d % 4 < 1 ? n : 1);
        }
        Console.Read();
    }
}
\$\endgroup\$
  • \$\begingroup\$ You can golf 1 byte by changing the while to for and inserting the int like this: for(int n=1;n<101;) \$\endgroup\$ – Kevin Cruijssen Oct 25 '16 at 14:40
  • \$\begingroup\$ You can even golf it some more like this: void k(){for(decimal f=1,d,n=1;n<101;)Console.WriteLine(f=((d=++n+f)+n*((d%2<1?1:0)-(d%3<1?1:0)))*(d%4<1?n:1));} (112 bytes) \$\endgroup\$ – Kevin Cruijssen Oct 25 '16 at 14:46
1
\$\begingroup\$

Batch, 110 bytes

@set n=0
@for /l %%i in (1,1,46)do @set/an=((n+=%%i)+(!(n%%2)-!(n%%3))*%%i)*(~-%%i*!(n%%4)+1)&call echo %%n%%

Uses @ETHproductions' formula, but tweaked slightly because Batch doesn't have ?:. Batch uses 32-bit signed integers so the loops stops at 46.

\$\endgroup\$
1
\$\begingroup\$

Perl, 75 Bytes

use bigint;$a+=$_,say$a=($a+($a%2?0:$_)-($a%3?0:$_))*($a%4?1:$_)for(1..100)

The code outputs each value on a new line, and computes all 100 values.

\$\endgroup\$
  • \$\begingroup\$ -Mbigint, no parenthesis around the 1..100, and !($a%2)*$_ instead of ($a%2?0:$_) (same for a%3..) should save a few bytes ;) \$\endgroup\$ – Dada Oct 29 '16 at 10:03
  • \$\begingroup\$ Gets it down to 60 bytes with those suggestions and a few other massages. \$\endgroup\$ – Xcali May 31 at 5:58
1
\$\begingroup\$

Haskell, 70 64 bytes

a%b=0^mod a b
n#i|s<-n+i=(s+s%2*i-s%3*i)*i^s%4
scanl1(#)[1..100]

scanl1(#)[1..100] returns the list with the first 100 elements. One byte less if I can stay in the 2^31 range (-> [1..46]).

scanl1 is like foldl1 but collects the intermediate results in a list. The divisibility tests are done via the helper function % which returns 0^0 = 1 if divisible and 0^x = 0 if not.

\$\endgroup\$
1
\$\begingroup\$

J, 46 bytes

(,{:((]^0=4|+)*(]*0=2|+)++-]*0=3|+)1+#)^:99]1x

Applies the method described in the challenge.

Usage

The extra command (,.~#\) is used to add indices to each value.

   (,.~#\) (,{:((]^0=4|+)*(]*0=2|+)++-]*0=3|+)1+#)^:99]1x
  1                            1
  2                            1
  3                           21
  4                           25
  5                           30
  6                          216
  7                          223
  8                          223
  9                         2169
 10                         2179
 11                         2190
 12                         2202
 13                         2215
 14                         2215
 15                         2245
 16                         2261
 17                         2295
 18                         2295
 19                         2333
 20                         2353
 21                         2395
 22                         2417
 23                        56649
 24                        56649
 25                        56699
 26                        56725
 27                      1533033
 28                      1533061
 29                      1533090
 30                     45993600
 31                     45993631
 32                     45993631
 33                   1517792001
 34                   1517792035
 35                   1517792070
 36                   1517792106
 37                   1517792143
 38                   1517792143
 39                   1517792221
 40                   1517792261
 41                   1517792343
 42                   1517792343
 43                   1517792429
 44                   1517792473
 45                   1517792563
 46                   1517792609
 47                  71336257041
 48                  71336257041
 49                  71336257139
 50                  71336257189
 51                3638149121841
 52                3638149121893
 53                3638149121946
 54              196460052588000
 55              196460052588055
 56              196460052588055
 57            11198222997525633
 58            11198222997525691
 59            11198222997525750
 60            11198222997525810
 61            11198222997525871
 62            11198222997525871
 63            11198222997525997
 64            11198222997526061
 65            11198222997526191
 66            11198222997526191
 67            11198222997526325
 68            11198222997526393
 69            11198222997526531
 70            11198222997526601
 71           795073832824398753
 72           795073832824398753
 73           795073832824398899
 74           795073832824398973
 75         59630537461829934225
 76         59630537461829934301
 77         59630537461829934378
 78       4651181922022734887568
 79       4651181922022734887647
 80       4651181922022734887647
 81     376745735683841525912529
 82     376745735683841525912611
 83     376745735683841525912694
 84     376745735683841525912778
 85     376745735683841525912863
 86     376745735683841525912863
 87     376745735683841525913037
 88     376745735683841525913125
 89     376745735683841525913303
 90     376745735683841525913303
 91     376745735683841525913485
 92     376745735683841525913577
 93     376745735683841525913763
 94     376745735683841525913857
 95   35790844889964944961834465
 96   35790844889964944961834465
 97   35790844889964944961834659
 98   35790844889964944961834757
 99 3543293644106529551221660545
100 3543293644106529551221660645
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 62 bytes

1,{((my \v=$_+my \n=++$+1)+n*(v%%2-v%%3))*(v%%4*n||1)}.../645/

Try it online!

REALLY had to work to get my byte count below those of the other non-golf-language solutions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.