New answers tagged

1

Keg, 11 bytes (SBCS) Now I realized that Keg is pretty hard to use... ¿Ï_^'(Ï_')" Try it online!


0

GolfScript, 12 bytes ~),(;{),(;}% Try it online!


0

W j f, 3 bytes It's actually pretty trivial in W ... M_M Explanation a % Take an input, say 3 M % Identity map every item in numeric value 3 % Since it is prohibited, a range is generated, % namely [1 2 3] a M % For every item in [1 2 3]: % Note that single items are 1, 2, and 3, *not* the whole list a % Generate a ...


2

Mathematica 46 42 bytes f=2^(#/2)HypergeometricU[-#/2,.5,-.5]I^-#& Try it online! Thanks to mabel for helping me shave 4 bytes by using what I think is an anonymous function. As seen in https://www.wolframalpha.com/input/?i=OEIS+A000085 and http://mathworld.wolfram.com/ConfluentHypergeometricFunctionoftheSecondKind.html


2

Python, 226 bytes Assumes the lists O and V are defined, as well as the time T, either an int or a float. Try it online s=lambda x:int(x/abs(x));r=range;f=lambda O,V,T:"".join(map(lambda t:t[1],sorted(sum([[((i-o)/v,"%+d"%(s(v)*c))for i in r((v>0)-(v<0)*(o==0),int((o+T*v)//1)+(v>0),s(v))]for c,o,v in zip(r(len(O)),O,V)],[]),key=lambda t:t[0]))) ...


1

Elm, 45 bytes f n = if n<2 then 1 else (n-1)*f(n-2)+f(n-1) Try it online! Implementation of the recursive function.


1

GolfScript, 16 bytes This is inspired by the Fibonacci GolfScript program. ~,1.@{)*1$+\}/\; Try it online! Explanation ~, # Generate exclusive range from input to 0 1. # Make 2 copies of 1 @ # Make the each target on the top { }/ # Foreach over the range ) # Increment the current item ...


1

PHP, 48 bytes function f($n){return$n<2?1:f(--$n)+$n*f($n-1);} Try it online! Implementation of the recursive function. Original version: 54 bytes (port of @Noodle9 answer): for($i=$j=1;++$n<$argn;$j=$k){$k=$i;$i+=$n*$j;}echo$i; Try it online!


2

Red, 53 bytes f: func[n][either 1 > n: n - 1[1][n *(f n - 1)+ f n]] Try it online!


1

Japt, 28 26 bytes _pZÌ+(ZÊÉ *ZgJÑ}g´U1õ ï)gJ Try it pZÌ+(ZÊÉ *ZgJÑ make sequence with next element from previous sequence _ ... }g´U repeat input -1 times 1õ ï) starting with [[1,1]] gJ implicit output last element of resulting sequence


0

Rogex, 27 bytes 90B300d00901e00100701a20f00 Try it online! Explained 90B 300 d00 # Set the loop value to 11 901 # Set the buffer to 1 e00 # While the buffer isn't equal to 11 100 701 # Print the buffer then increment it a20 # Print a space f00 # End loop Rogex answer number 2! Also, my 101st answer! (´• ヮ •`)


3

C (gcc), 40 bytes long f(long n){n=n<2?1:f(--n)+n*f(n-1);} Try it online! Merely copied from @Arnauld answer


1

Retina 0.8.2, 80 bytes .+ $*_;;_; {`^_(_*;)(_*;_+) $1_$2,$2 +`(,_*)_(;_+;(_*))|, $3$1$2 ^;_*;(_*).* $.1 Try it online! Explanation: .+ $*_;;_; Initialise the work area with n, i=0, and T=[1] (note that the list indices are reversed, so that the first element of T is T[i] and the last is T[0]) converted to unary. {` Loop n times (actually until the ...


2

J, 25 bytes ($:+]*$:@<:)@<:`1:@.(<&2) Try it online! $: is J's recursion operator, so this is a fairly straightforward impl of the recursive def. I tried a couple other approaches (including using ^: power of operator) but wasn't able to get shorter than this. Am curious if anyone can improve it...


1

Keg, 21 bytes :1≤[_1|;:@Tƒ^:;@Tƒ*+] Try it online! Simply implements the formula in a recursive manner. The footer is mainly for testing purposes


2

naz, 50 bytes 1x1f1a1o2x1v0m9a1a1o1v0x1f1f1f1f1f1f1f1f1f8s1o1s1o Explanation (with 0x commands removed) 1x1f # Function 1 1a1o # Add 1 to the register and output 2x1v # Store the new value in variable 1 0m9a1a1o # Output a newline 1v # Read variable 1 into the register ...


3

Haskell, 29 bytes f n|n<2=1|m<-n-1=f m+m*f(n-2) Try it online! Implements the recursive formula.


20

Python, 33 bytes f=lambda n:n<2or~-n*f(n-2)+f(n-1) Try it online! Uses the recursive formula.


1

Charcoal, 20 bytes ⊞υ¹FN⊞υ⁺§υι×ι§υ⊖ιI⊟υ Try it online! Link is to verbose version of code. Uses the recurrence relation. Explanation: ⊞υ¹ T(0)=1 FN Loop n times. ⊞υ⁺§υι×ι§υ⊖ι T(i+1)=T(i)+iT(i-1) I⊟υ Output T(n).


8

Jelly, 6 bytes Œ!ỤƑ€S A monadic Link accepting a non-negative integer which yields a positive integer. Try it online! Or see the first 10 (n=10 is too slow) How? Builds all permutations of [1...n] (or if n=0 just [[]]) and counts the number which are involutions. Œ!ỤƑ€S - Link: integer, n e.g. 0 3 Œ! - all permutations [[]] ...


0

Lua, 96 bytes n=io.read()i=0 while i do for j=0,n..i do if (n..i)+0==j*j then print(i)return end end i=i+1 end Try it online!


7

TI-BASIC, 28 22 bytes ∑((Ans nCr (2K))(2K)!/(2^KK!),K,0,int(.5Ans Input is n in Ans. Output is the nth telephone number. I was going to use the first formula mentioned in the challenge, but testing it resulted in ERROR: OVERFLOWs even on smaller numbers because TI-BASIC handles n!! as two successive factorials instead of a double factorial. At least it ...


12

Oasis, 7 6 5 bytes àn*+1 Try it online! # to compute the nth telephone number f(n): à # push the telephone numbers f(n-1) and f(n-2) n # push n * # multiply: n * f(n-2) + # add: n * f(n-2) + f(n-1) 1 # base case: f(0) = 1


2

Perl 6, 26 bytes {{(1,1,++$×*+*...*)[$_]}} Try it online!


5

JavaScript (ES6), 25 bytes 0-indexed. Uses the recurrence relation. f=n=>n<2||f(--n)+n*f(n-1) Try it online!


9

cQuents, 10 bytes =1:Z+Y($-2 1-indexed. Try it online! Explanation =1 first term in sequence is 1 : given n, output nth term in sequence (1-indexed) each term is Z+ (n-1) term + Y (n-2) term * ($-2 (index - 2)


3

Python 3, 81 \$\cdots\$ 57 56 bytes def f(n): a=b=i=1 while i<n:a,b=a+i*b,a;i+=1 return a Try it online! Uses 0 based indexing and implements the recursive formula.


4

05AB1E, 12 8 bytes -4 bytes by porting Stephen's cQuent answer 1λèsN<*+ Try it online!


3

Brachylog, 8 bytes ;.c~^₂∧ℕ Sadly enough it takes 3 bytes to check if a number is square and an extra ℕ is needed if the input is a square itself. Explanation ;.c~^₂∧ℕ # Find a value ;.c # that concatenate to the input ~^₂ # gives a square number ∧ℕ # and make sure that value is a number # (otherwise ...


1

05AB1E, 8 7 bytes PLIδÖʒO Inspired by @jimmy23013's CJam answer, so make sure to upvote him as well! Input as a pair of integers \$[m,n]\$ and output as a list of [0,1] and [1,0] pairs for \$V\$ and \$H\$ respectively. Try it online or verify all test cases. Explanation: P # Take the product of the (implicit) input-pair: m*n L # Pop and ...


0

GolfScript, 24 bytes I don't feel right to have a Burlesque answer without a GolfScript answer.(Please don't mind if this is a little bit slow, although they seem to produce the right result.) -1{).2$\+~.),{.*}%?)!}do Try it online! Explanation -1 # The counter for the current number (it is >= 0) { # Do at ...


0

PHP, 50 bytes <?=array_sum(str_split(decbin($argn&-2^$argn*2))); Try it online! Unfortunately, really don't think I can get it any more golfed than this! The idea is to count transitions 1->0 or 0->1.


2

Pyth, 18 16 15 11 10 bytes Uses the floating point square root test, like most of the other answers. As such, the precision is eventually insufficient, and the smallest square it fails with is 4503599761588225 – with an input of 45035997, it returns 61588224 instead of 61588225 as it should. The smallest input it fails with is 20256971, returning 228498166 ...


3

Pyth, 35 33 30 23 21 bytes This is a program that uses only integer arithmetic. Since Python has arbitrary-precision integers, this is guaranteed to work for arbitrarily large numbers, whereas the square-root test used by most of the other answers fails with numbers greater than 252, having false positives with numbers very close to a perfect square. ...


4

Python 2, 47 49 bytes f=lambda p,i=0:int(`p`+`i`)**.5%1and f(p,i+1)or i Try it online!


5

C++ (clang), 194 \$\cdots\$ 128 129 bytes #import<string> #define z std::to_string int f(int n){for(int j=-1;;)for(long i=0,m=stol(z(n)+z(++j));i++<m;)if(i*i==m)return j;} Try it online! Added a byte to fix overflow error kindly pointed out by Deadcode.


2

Japt, 10 bytes _siU ¬%1}f Saved a byte thanks to @AZTECCO Try it


4

C, 123 115 bytes (thanks, Jo King!) n,k,m=10,x,p;main(){scanf("%d",&n);for(;k<m||(m*=10);++k)for(p=0;x=n*m+k,p<=x;++p)if(p*p==x)return printf("%d",k);} I noticed this version can fail on not-very-large inputs due to integer overflow. So I, did a separate version, with less overflow. C, less overflow. 117 bytes n,k,m=10,x,p;main(){scanf("%d",&...


1

Charcoal, 16 bytes ≔⁰ηW﹪₂I⁺θη¹≦⊕ηIη Try it online! Link is to verbose version of code. Explanation: ≔⁰η Initialise the result to zero. W﹪₂I⁺θη¹ Repeat until the concatenation is a square. ≦⊕η Increment the result. Iη Output the result. 36 bytes for an efficient solution: ≔×χNη Try it online! Link is to verbose version of code. Explanation: ...


1

cQuents, 13 bytes #|1:#bA~N ?$$ Try it online! Explanation #|1 output nth element in sequence (remove to output indefinitely) : mode: sequence, output nth element in sequence # conditional: count N up, when true, add to sequence b call second line, passing A~N input concatenated to ...


3

Perl 5 -pal, 30 bytes $_=0;$_++while"@F$_"**.5=~/\./ Try it online!


1

Perl 5, 35 40 bytes $k=0;L:$_="$n$k"**.5;!/\./ or$k++,goto L Try it online!


2

PHP, 53 47 44 bytes for($k=-1;fmod(sqrt($argn.++$k),1););echo$k; Try it online!


0

Haskell, 93 88 bytes [j|i<-(show.(^2))<$>[0..],i/=n,n`isPrefixOf`i,j<-[drop(length n)i],j=="0"||j!!0/='0']!!0 Try it online!


4

R, 45 bytes n=scan();while((paste0(n,+F):1)^.5%%1)F=F+1;F Try it online! Throws a warning for each iteration noting that it only uses the first element of the vector as a loop condition. This will run out of space for large inputs since it constructs a vector of length \$n'k\$ at each iteration; this version will work for larger inputs and should be a bit ...


2

Japt, 10 bytes @s+X ¬v1}a Try it


1

Jelly, 8 bytes 0ṭVƲɗ1# Try it online! A monadic link taking as its argument an integer and returning an integer. Explanation 0 ɗ1# | Starting with zero, find 1 integer where the following is true as a dyad, using the original argument as the right argument ṭ | - Tag onto the end V | - Evaluate after converting to string and ...


0

PHP, 83 86 bytes g:substr($k=$i*++$i,0,$l=strlen($n=$argn))==$n&&("$k")[$l]&&die(substr($k,$l));goto g; Try it online! -9 bytes thanks to @640KB and bugs fixed (+12). Only fails for case 10 currently. Ungolfed <?php $number=$argv[1]; $len = strlen($number); $i=0; do { $k = $i*$i; if (substr($k,0,$len)==$number && ...


3

Ruby -pl, 43 bytes i=1;i+=1until"#{i*i}"[/^#$_(?!0.|$)/] $_=$' Try it online! Tests each square until it finds one that starts with the input and is followed by a number with no leading zero. I thought going in this direction rather than the more natural one would be shorter, but now I'm doubting that it is.


1

JavaScript (Node.js), 38 Bytes Try it Online! f=(a,b=[0])=>(a+b)**.5%1?f(a,[++b]):+b


Top 50 recent answers are included