New answers tagged

4

Python 3.8, 53 51 bytes (thanks @user for pointing out extra spaces) def r(n): i=n while(a:=n**(1/i))%1:i-=1 return a Try it online!


3

05AB1E, 6 bytes Inspired by SunnyMoon's answer. LÀ.ΔBR Try it online! L # push the range [1, 2, ..., n] À # rotate the 1 to the back: [2, 3, ..., n, 1] .Δ # find the first integer where ... B # the input converted to that base R # reversed # implicit: is equal to 1 as a number 05AB1E, 8 bytes LÀ.Δ.n.ï ...


2

05AB1E, 12 (10) bytes ÓDā<Ør0š¿÷mP Port of @UnrelatedString's Jelly answer. The 0š shouldn't be necessary, but unfortunately there is a bug in 05AB1E for ¿ with empty lists. Try it online or verify all test cases. Explanation: Ó # Get the prime exponents of the (implicit) input-integer D # Duplicate this list of exponents ā ...


5

R, 37 33 bytes -4 bytes thanks to Dominic van Essen match(T,!log(n<-scan(),1:n)%%1,1) Try it online! A different (and shorter) approach than the one used in Giuseppe's and Dominic van Essen's R answers. Finds the first integer k such that log(n,k) is an integer, or returns 1 if there is no such k (which corresponds to the special case n=1).


0

Perl 5 -p, 40 bytes print$_,("@{[1-$_..0]}"=~/\d/g)[0..$_-1] Try it online!


1

PowerShell Core, 45 bytes param($a)"$a$(($a-1)..0-join''|% su* 0 $a)$a" Try it online! Explanation ($a-1)..0-join''|% su* 0 $a # builds an array from a-1 to 0, joins them with no separators # and takes the `a` first chars "$a ... $a" # surrounds the result with the parameter `...


0

Stax, 9 8 7 bytes îσQçφ╠♦ Run and debug it -1 byte from recursive. Explanation (Unpacked) rr$x%sy|S r range 0..n-1 r reversed $ converted to string x( take n characters y push n as string from register y |S prepend and append it to the range implicit output


3

Stax, 10 bytes ┌Pèó~JRå▲ï Run and debug it Explanation R{xs|E|+1=}j R range 1..n { }j get first number i where: xs|E input(x) in base i digits |+ summed 1= equals 1


3

Factor, 49 bytes [ dup [1,b] 2dup '[ _ swap _ n^v member? ] find ] Try it online! Slow for larger inputs because it tries to evaluate a large power. [ dup [1,b] 2dup ! ( n 1..n n 1..n ) '[ ! Put stack items in the `_`s in the quotation _(n) swap _(1..n) ! ( elt -- n elt 1..n ) n^v member? ! Test if n appears ...


1

Wolfram Language (Mathematica), 36 bytes #^Last[#<2||1/GCD@@FactorInteger@#]& Try it online! -11 bytes from @att


5

JavaScript (ES7), 36 bytes Recursively looks for the highest \$i\le n\$ such that \$k=n^{1/i}\$ is an integer. Then returns this \$k\$. f=(n,i=n)=>(k=n**(1/i))%1?f(n,i-1):k Try it online! JavaScript (ES7), 37 bytes A slightly longer version that performs more recursive calls but is not subject to rounding errors. n=>(g=k=>k**i-n?g(k-1||i--|n):k)(...


4

R, 47 bytes n=scan();match(n,sapply(0:n,"^",1:n))%/%n-(n<2) Try it online! Struggled for ages trying to beat Giuseppe's answer, only to be totally outgolfed (seconds before posting) by Robin Ryder's comment (now an answer)...


2

Octave, 33 bytes @(n)[~,j]=find((t=1:n)'.^t'==n,1) Try it online! How it works @(n) % anonymous function with input n (t=1:n) % let t = [1, 2, ..., n] (row vector) .^ % element-wise power with broadcast... ' % of t transposed... ...


7

Husk, 8 bytes VȯεΣ`B¹ḣ Try it online! V # index of first truthy element of ȯ # applying 3 functions to ḣ # 1...input `B¹ # convert input to this base Σ # sum of digits ε # is at most 1


3

Japt -g, 10 bytes I feel like I'm missing a trick here. õ ï æ@¥Xrp Try it õ ï æ@¥Xrp :Implicit input of integer U õ :Range [1,U] ï :Cartesian product with itself æ :Get first pair that returns true @ :When passed through the following function as X ¥ : Test U for equality with Xr ...


2

Charcoal, 19 16 bytes NθI⊕⌕Eθ⎇ιΣ↨θ⊕ιθ¹ Try it online! Link is to verbose version of code. Edit: Now back to a reformulation of my original answer. Works by converting n to each base 1..n and finding the first 1-indexed value with a digit sum of 1. Conveniently this automatically works for an input of 0 (the resulting list is empty, so the 1-indexed position ...


5

Retina 0.8.2, 60 bytes .+ $* (?<=(?=((?=((1*)(?=\5\3+$)1)(\2*$))\4)*1$)^(..+)).* 1 Try it online! Link includes test cases. Explanation: .+ $* Convert to unary. (?<=(?=...$)^(..+)).* Delete the earliest suffix leaving behind the smallest prefix \$r\$ (captured into \5) such that the \$n\$ matches the following: ((?=((1*)(?=\5\3+$)1)(\2*$))\4)*1 ...


10

J, 14 bytes (%+./)&.(_&q:) Try it online! (%+./)&.(_&q:) &.(_&q:) number to prime exponents (%+./) divide them by their GCD &.(_&q:) prime exponents to number


3

C (gcc), 65 60 58 bytes Saved 5 bytes thanks to Sisyphus!!! p;i;r;f(n){for(r=0;r++<n;)for(p=i=1;i++<n;p*=r)n=p-n?n:r;} Try it online!


5

k4, 26 24 21 bytes -2 bytes by ignoring n=0 case -3 bytes by applying @caird coinheringaahing's logic {(+/'(!x).q.vs\:x)?1} Benefits from list ? value returning the count of the list if the value isn't present in it, and from convenient weirdness with the n=0 and n=1 edge cases.


2

Scala, 50 bytes Back to 50 bytes because n=0 doesn't have to be handled anymore! n=>1.to(n)find(r=>1.to(n)exists(n==math.pow(r,_))) Try it online!


7

Python 3, 55 \$\cdots\$ 59 57 bytes Added 7 bytes to fix an error kindly pointed by user. Saved 3 bytes thanks to user!!! lambda n:{r**i:r for i in range(n)for r in range(n+1)}[n] Try it online!


0

05AB1E, 10 bytes Î<ŸRJ¹£¹.ø Try it online! Î<ŸRJ¹£¹.ø # full program .ø # surround... £ # first... ¹ # input... £ # chars of... J # joined elements of... Ÿ # [... Î # 0 ... input... < # minus 1 Ÿ # ]... R # reversed... .ø # with... ¹ ...


8

Brachylog, 6 bytes 1|~^hℕ Try it online! 1|~^hℕ with the implicit input n 1 input and output is 1 | or ~^ find two numbers [r, i] so that r^i = n h return r ℕ to limit the search space: r must be positive Search tries lowest i first, so we get the maximum r for free.


7

05AB1E, 8 11 8 bytes -3 thanks to @ovs! L¦BíCXkÌ Try it online! I am trying to somehow implement a log function to check whether a number matches the regex 10*, but that is too mathematical for me... Wait, how? L # Push all numbers natural numbers up to input [1, 2, 3 ... I] ¦ # What is that 1 doing there? Remove it! ...


4

R, 49 44 bytes n=scan();which(outer(1:n,n:1,"^")==n,T)[1,1] Try it online! Thanks to Dominic van Essen for pointing out a bug. n <- scan() # read input arr <- outer(0:n,1:n,"^") # create the array of powers (0^(1:n), 1^(1:n), ... n^(1:n)) arr <- t(arr) # transpose, so the array is ((0:n)^1, (0:n)^2, .....


8

Jelly, 10 bytes ÆEgƒ0:@ƊÆẸ Try it online! ÆE:g/$ÆẸ errors given 1. ÆE Take the exponents of the input's prime factorization. :@Ɗ Divide each exponent by gƒ0 the exponents' GCD (or 0 in the case that there are none). ÆẸ Let the result be the exponents of the output's prime factorization.


5

Jelly, 8 6 5 bytes bR§i1 Try it online! Uses the fact that n in base r has the format 100...000, meaning that the sum of the digits only equals 1 in base r -1 byte (indirectly) thanks to Dominic van Essen's answer, make sure to give them an upvote How it works bR§i1 - Main link. Takes n on the left R - [1, 2, 3, ..., n] b - Convert n to each base 1, ...


2

Jelly, 10 bytes ŒṗḟÐḟÆḞ€ṪP Try it online! Times out for \$n > 60\$ How it works ŒṗḟÐḟÆḞ€ṪP - Main link. Takes n on the left Œṗ - Integer partitions of n ÆḞ€ - First n Fibonacci numbers Ðḟ - Keep the partitions p which yield an empty list under: ḟ - Remove all elements of p which are in the first n Fibonacci numbers ...


0

Jelly, 11 bytes ḶUDFṁ;D;@DḌ Try it online! There is also this kinda cheaty 9 byter, which only works due to how the Y command in the Footer works, so I'm not counting that. Jelly's dislike of strings really hurts it here, and CJam's stack is just helpful enough to beat Jelly. How it works ḶUDFṁ;D;@DḌ - Main link. Takes n on the left Ḷ - [0, 1, 2, ....


0

Scala, 80 bytes n=>Stream.iterate(1)(n*_)map(p=>p to p*n-1 find(x=>2*x==x%n*p+x/n))find(_!=None) Try it online! Takes an integer n, returns an Option[Option[Int]] (really a Some[Some[Int]]).


0

SNOBOL4 (CSNOBOL4), 71 bytes Y =1 R X =X + 1 Y =Y * X GT(X,Y) :S(O) Y =Y / X O OUTPUT =Y :(R) END Try it online! Prints the sequence forever.


1

Crystal, 41 bytes (-2 bytes thanks to Giuseppe) def a(n)n<2?1: n>(f=a(n-1))?n*f: f//n end Try it online!


2

PowerShell Core, 65 bytes Output the nth value: $r=1 1..$args[0]|%{$r=([Math]::Floor($r/$_),($r*$_))[$_-gt$r]} $r Explanations $r=1 # Initialises the result as 1 1..$args[0]|%{ # For 1 to the argument $r=( # In an array calculate the two possible values [Math]::Floor($r/$_), # ...


1

Jelly, 5 bytes UQU⁻Q Try it online! Just another port of xnor's answer, so go upvote that. How it works UQU⁻Q - Main link. Takes a list l on the left U - rev(l) Q - unique(rev(l)) U - rev(unique(rev(l))) Q - unique(l) ⁻ - rev(unique(rev(l)) ≠ unique(l)


4

Java (JDK), 52 bytes n->{int i,a=i=1;for(;i++<n;)a=i>a?i*a:a/i;return a;} Try it online! Note: Thanks @RedwolfPrograms for -1 Byte and @user for -10(?) bytes.


1

R, 93 92 bytes Edit: -1 byte thanks to Giuseppe f=function(x)`if`(sum(x|1)<2,(-1)^(sum(x)!=2),`if`(!all(x%in%1:2),-1,(y=f(rle(x)$l))+(y>0))) Try it online! Returns 1-based number of iterations for truthy input, and -1 for all falsy inputs. This needed quite careful input testing, especially for the last two test cases... Commented: f=function(x) ...


0

Scala 3, 460 bytes This may be the longest answer here, but it's also the fastest one here...because it works at compile time. import compiletime.ops.int._ type r[T,P]=T match{case h*:t=>r[t,h*:P]case E=>P} type E=EmptyTuple type F[K,R,L,S,P,I]=K match{case E=>L-S match{case 0=>R match{case E=>0 case _=>F[r[R,E],E,0,0,0,I+1]}case _=>0}...


4

Husk, 6 bytes Ḟ§|*÷ṫ Try it online! How? Ḟ§|*÷ṫ - function: integer, n ṫ - reversed range -> [n,n-1,...,2,1] Ḟ - right-fold using this f(a,b): § - fork: | - v logical-OR w ÷ - v: b integer-divide a * - w: a multiplied by b


3

Hy, 54 bytes (defn f[n](or(+(= n 1))(//(f(- n 1))n)(*(f(- n 1))n))) Try it online! This function outputs the nth value of the sequence. Ungolfed version: (defn f [n] (or (+ (= n 1)) (// (f (- n 1)) n) (* (f (- n 1)) n)))


1

Stax, 26 bytes äE⌐+É7∩ΦΓyr╔ßΣ·φƒÇe►ef%I» Run and debug it Tried with a generator, but it seems like a while loop is shorter.


3

APL (Dyalog Unicode), 56 54 49 bytes (SBCS) Saved 2 7 bytes thanks to @ovs! 0∘{×⍵:r(,⍣d)(r,⍺)∇⍵-d←⍺∊⍨r←(⊃((≤∨⍺∊⍨-)⌷-,+)≢)⍺⋄⍬} Try it online! Can be f k. Recursively builds up the first k duplicates. ⍺ holds Recamán's sequence (in reverse), and is set to 0 if no argument is given (at the start). If k (⍵) is 0, it returns an empty array (⍬). Otherwise, it ...


5

Forth (gforth), 51 bytes : f 1+ 1 tuck ?do i 2dup <= if * else / then loop ; Try it online! Code Explanation : f \ start word definition 1+ \ add 1 to n 1 tuck \ set up accumulator and loop parameters ?do \ loop from 1 to n (if n > 1) i 2dup \ set up top two stack values and duplicate <= if \ if a(n-1) <= n ...


0

Jelly, 23 bytes “J,1’D=þD§S×ẠƊ 0Ç<CɗƓ#Ṫ Try it online! This takes h on the command line and n on STDIN How it works 0Ç<CɗƓ#Ṫ - Main link. Takes h on the left ɗ - Group the previous 3 links into a dyad f(k, h): Ç - Call the helper link on k < - Less than h? C - Complement; 0 -> 1, 1 -> 0 0 Ɠ# - Read an ...


4

MathGolf, 11 9 bytes 1k{î`<¿*/ -2 bytes thanks to @ovs. Outputs the \$n^{th}\$ value. Try it online. Explanation: 1 # Push 1 k{ # Loop the input amount of times: î # Push the 1-based loop index ` # Duplicate the top two items <¿ # If the current value is smaller than the 1-based loop index: a(n-1)<n: ...


0

Perl 5 -p, 82 bytes $;{1}++;map@;{$_*2+1,$_*3-1}=1,keys%;while$_*3>keys%;;$_=(sort{$a-$b}keys%;)[$_-1] Try it online!


2

Pyth, 20 bytes J1V}2hQJ=J?>NJ*NJ/JN Try it online! 1-indexed, outputs the first n values.


0

Husk, 17 bytes !OuΞ¡ṁ§eo←*3o→D;1 Try it online! There should be a shorter way using set difference. This answer creates all the possibilities, merges them and deduplicates.


0

R, 122 bytes function(n)while(n){T=T+1 m=T^2 t=0 while(m<-m-1){i=s=0;while((i=i+1)<m)if(!m%%i)s=s+i;t=t+(T==s)} if(!t){print(T);n=n-1}} Try it online! Anyone familiar with R programming will certainly wince when they look at this. Loops in general are horribly inefficient in R, and the fast and idiomatic way to tackle this kind of question would ...


1

Jelly, 16 bytes ŒṖḌI’ḂƑƊƇḢ.ịr/ḟƲ Try it online! Input as a list of digits. Outputs a singleton list [a] containing the missing element, or [] if there isn't one. +1 byte to output 0 instead, and +1 byte to input as a single integer. How it works ŒṖḌI’ḂƑƊƇḢ.ịr/ḟƲ - Main link. Takes a list of digits D on the left ŒṖ - All partitions of D Ḍ ...


Top 50 recent answers are included