Your program must print out this exact sequence:
1
2
4
10
24
54
116
242
496
1006
2028
4074
8168
16358
32740
65506
131040
262110
NOPE!
As usual, shortest program wins. Standard code golf rules apply.
i=0;exec"print 2**i-i<<(i>0);i+=1;"*18;print"NOPE!"
Notices that the formula is 2(2n-n), for all n except 0.
Saved two bytes thanks to Wheat Wizard (i<<1-(i<1)
changed to i<<(i>0)
, duh!).
i=0;exec"print[2**i-i<<(i>0),'NOPE!'][i>17];i+=1;"*19
for i in range(18):print(2**i-i)*2-(i<1) print"NOPE!"
solution, but was too late, it seems :)
\$\endgroup\$
Commented
Dec 27, 2017 at 20:38
Compiled with gfortran
, with the -cpp
flag to enable preprocessing, or saving the file with .F95
extension.
#define p print*,
program w
p 1
do i=1,17
p 2*(2**i-i)
enddo
p'NOPE!'
end
Longest answer! Not anymore! YAY!
17Rµ2*_×2“NOPE!”ṭ1;Y
Explanation
17Rµ2*_×2“NOPE!”ṭ1;Y
17R range(17):[1,2,...17]
µ new monadic link
2* exponentiate: [2*1, 2*2,...2*17]
_ subtract: [2*1-1, 2*2-1,...2*17-17]
×2 times 2. Ḥ would need an additional syntax character but ×2 doesn't.
“NOPE!”ṭ append "NOPE!" to the above list
1; prepend 1 to the above list
Y join by newlines and implicitly print
A recursive Jelly solution, 26 bytes
’ß+_3$ḤµṖṖ$¡
18RÇ€Y⁷“NOPE!
Took the Y⁷“NOPE!
trick from Jonathan Allan.
Ḥ
is a monad Jelly applies it to the right argument of _
.
\$\endgroup\$
⁸
but no save there either
\$\endgroup\$
Commented
Dec 27, 2017 at 20:55
1O'²∫ūF-«O"NOPE!
Explanation:
1O output 1
'²∫ 17 times do, pushing 1-indexed counter
ū calculate 2^pop
F- subtract from that the counter
« multiply by 2
O output
"NOPE! push "NOPE!", which gets printed at the end as implicit output hasn't been disabled. This gets pushed every iteration of the loop, to save a byte
Saved 2 bytes thanks to @Giuseppe (replace 2*(i-2)
by 2*i-4
)
This version prints exactly as required:
a=1;for i=1:18,fprintf('%d\n',a),a=2*a+max(2*i-4,0);end,disp('NOPE!')
This version prints the same numbers and the NOPE!
, but with a different formatting:
a=1,for i=1:17,a=2*a+max(2*i-4,0),end,disp('NOPE!')
max(2*(i-2),0)
is equivalent to max(2*i-4,0)
which will save a byte or two
\$\endgroup\$
X17LDoα·»"NOPE!"»
Explanation
X # push 1
17L # push range [1 ... 17]
D # duplicate
o # raise 2 to the power of each
α # absolute difference
· # times 2
» # join list on newlines
"NOPE!" # push this string
» # join stack on newlines
Saved 1 byte thanks to @l4m2
Saved 1 byte thanks to @user71546
for(i=0;i<18;l(2**i-i<<!!i++))l=console.log;l("NOPE!")
Uses the formula 2(2n-n)
for all n ≠ 0
and 2n-n
for n = 0
, based on @MrXcoders Python 2 answer
_=>[...""+1e19].map((_,i)=>i>18?"NOPE!":2**i-i<<!!i)
l(i=1);while(i<18)
=> for(l(i=1);i<18;)
\$\endgroup\$
(2<<i)-i-i++
to 2**i-i++<<1
for 1 byte less ;)
\$\endgroup\$
Commented
Dec 28, 2017 at 8:16
1⁷17µ2*_µ€ḤY⁷“NOPE!
A full program printing the result to STDOUT.
1⁷17µ2*_µ€ḤY⁷“NOPE! - Main link: no arguments
1 - literal 1 (printed as next byte starts a new leading constant chain)
⁷ - literal newline (also printed for the same reason)
17 - literal seventeen
µ µ€ - for €ach n in range -> [1,2,3,...,17]
2 - literal two
* - raise to power n -> [2,4,8,...,131072]
_ - subtract n -> [1,2,5,...,131055]
Ḥ - double (vectorises) -> [2,4,10,...,262110]
Y - join with newlines (again this is printed, as above)
⁷ - literal newline (this is also printed for the same reason)
“NOPE! - literal list of characters "NOPE!" (no trailing close quote required
- at the end of a Jelly program)
- implicit print
Some alternatives exist for 19 bytes, like
‘Ṅ+⁴2*$_$€ḤY⁷“NOPE!
@echo 1
@set/ap=2
@for /l %%i in (0,2,32)do @call echo %%p%%&set/ap+=p+%%i
@echo NOPE!
00000000: 04c1 4101 4331 0042 b1fb 5303 94f2 3bff ..A.C1.B..S...;.
00000010: c696 9850 2c52 6eb1 471a fa1b 9646 9447 ...P,Rn.G....F.G
00000020: f595 e73d bc73 1f27 5fc5 eed5 f0b1 2ab2 ...=.s.'_.....*.
00000030: d8e2 3f3f ff00 5745 00 ..??..WE.
a->{var r="";for(int i=0;i<18;)r+=(i<1?1:2)*((1<<i)-i++)+"\n";return r+"NOPE!";}
Using the formula from Herman Lauenstein's answer.
Thanks to Kevin Cruijssen for golfing 14 bytes.
Try it online here.
Ungolfed version:
a -> { // lambda taking an unused null parameter
var r = ""; // we build the output as a String
for(int i = 0; i < 18; ) // generate the sequence
r += (i < 1 ? 1 : 2) * ((1 << i) - i++) + "\n"; // append 2*(2^i-i) for n > 0, 2^i-i for n = 0
return r + "NOPE!"; // return the constructed sequence, with NOPE! appended
}
var o=System.out;
. I need to remember that one! In this case it's shorter to return a String though: Try it online 80 bytes.
\$\endgroup\$
Commented
Mar 29, 2018 at 10:20
interface M{static void main(String[]a){...}}
is missing. ;p Since program/function, print/return, and flexible I/O are default when undefined explicitly I usually use that. If the challenge would have stated something alone the lines of "Create a program (no function) that prints the following exactly to STDOUT." I would follow that, but since it's only loosely mentions "program" and "print" without any explicit rules I'll just use the default, in which case a lambda function returning a String (or char-array for all I care) is fine. :)
\$\endgroup\$
Commented
Mar 29, 2018 at 11:19
print
is shorter than return
), etc. Which is why most languages indeed print. (PS: With older challenges before these defaults were properly defined you will see a lot of full programs, even for Java, and challenges almost always mention program in the description. I usually answer both a program and function in that case, like this answer I recently posted.)
\$\endgroup\$
Commented
Mar 29, 2018 at 11:23
i=0
x=1
print 1
exec"x*=2;print x;x+=i;i+=1;"*17
print"NOPE!"
1 2 4
, while the other starts 1 2 2 4
.
\$\endgroup\$
unlines(
instead of (unlines$
saves a byte.
\$\endgroup\$
import StdEnv
Start=("1\n",[(2*(2^n-n),'
')\\n<-[1..17]],"NOPE!")
Trivia: The type that is actually being printed is (String, [(Int, Char)], String)
E¹⁸I⎇ι⊗⁻X²ιι¹NOPE!
Try it online! Link is to verbose version of code. Explanation:
¹⁸ Literal 18
E Map over implicit range
² Literal 2
ι Current value
X Exponentiate
ι Current value
⁻ Subtract
⊗ Double
¹ Literal 1
ι Current value
⎇ Ternary
I Cast to string
Implicitly print on separate lines
NOPE! Literal string
Implictly print
echo 1
for((i=0;i++<17;));do
echo $((2*(2**i-i)))
done
echo "NOPE!"
DifferenceRoot[Function[{y,n},{4-2 n-2 y[n]+y[1+n]==0,y[1]==1,y[2]==2}]]/@Range@18
This is actually a holonomic sequence whose solution was obtained from
f = FindSequenceFunction[{1, 2, 4, 10, 24, 54, 116, 242, 496, 1006}]
DifferenceRoot[Function[{y,n},{4 - 2 n - 2 y[n] + y[1 + n] == 0, y[1] == 1, y[2] == 2}]]
I don't know why the NOPE!
is needed as the sequence does continue
f[19]
524252
OUTPUT =1
I X =X + 1
OUTPUT =LT(X,18) 2 * (2 ^ X - X) :S(I)
OUTPUT ='NOPE!'
END
I'm still not really sure why I had two counters going; I only needed one.
Thanks to @Mr. Xcoder for saving three bytes!
f(n){for(n=-1;++n<18;)printf("%d\n",((1<<n)-n)<<!!n);puts("NOPE!");}
<<!!n
saves two bytes instead of <<(n>0)
\$\endgroup\$
Commented
Dec 28, 2017 at 13:19
E++A1mB17"D~^2a"A"NOPE!
E++A1mB17"D~^2a"A"NOPE! - Implicitly print the evaluated value
E - Join with newline
+ - Concatenate
+A1mB17"D~^2a" - [1, 2, 6, 14... 262142]
+ - Concatenate
A1 - Listify 1 [1]
mB17"D~^2a" - [2,6,14... 262142]
m - map with
B17 - [1,2...17]
"D~^2a" - Function 2(2^n - 1)
A"NOPE! - Listify ["NOPE!"]
{1,{2*(2**++$-++$)}.../21/,'NOPE!'}
{ # bare block lambda
1, # start the sequence with `1`
{ # bare block used to generate values
2*(
2 ** ++$ # 2 to the power of an incrementing state variable
-
++$ # incrementing anonymous state variable
)
}
... # keep generating values until
/21/, # it generates a value that contains '21'
'NOPE!' # continue sequence with string 'NOPE!'
}
->{[1]+(1..17).map{|i|2*(2**i-i)}<<"NOPE!"}
A lambda returning an array of integers (and one string). Breaking out 1 into a special case was pretty cheap, and didn't hurt my brain at all!
NOPE!
? Once you have the sequence you can continue it. \$\endgroup\$NOPE!
just makes each submission longer by about 10 bytes, and doesn’t serve any purpose when it comes to making the challenge more interesting. \$\endgroup\$