New answers tagged

0

Scala, 163 bytes n=>{val a=(2 to 2019).map(x=>(x,((1 to x/2).filter(x%_==0)).max)).foldLeft(Seq(1))((l,e)=>l.patch(l.indexOf(e._2)+1,Seq(e._1),0));val i=a indexOf n;(a(i-1),a(i+1)) Try it online!


0

Japt, 36 bytes 1k #É9ò2@iUaXâ n- gJÑ)ÄX g[J1]c+UaNg Try it 1k assign [1] to U #É9ò2 pass range 2...2019 to function: @i ... X insert each value in U at index: Ua find max divisor of X in U: Xâ divisors n- sorted gJÑ)Ä 2nd to last g get elements at ...


1

R, 90 89 bytes for(i in 2:2019)T=append(T,i,match(max(which(!i%%1:(i-1))),T));T[match(scan(),T)+c(-1,1)] Try it online! Builds up the list into T and extracts the relevant values. As a bonus, it works for 1 and almost works for 1024, as R's 1-based indexing returns nothing for index 0 and NA for index 2020.


0

Retina 0.8.2, 109 bytes ^ 2019$*_ _ $`_¶ (_+)\1+¶ $1 $& +m`^(_+)¶((_+¶)*)\1 (_+¶) $1¶$4$2 _+ $.& ^(.+¶)*(.+)¶(.+)(¶.+)(¶.+)*¶\3$ $2$4 Try it online! Note: Link only goes to 201 as 2019 is too slow for TIO. Explanation: ^ 2019$*_ Prepend 2019 in unary. _ $`_¶ Count from 1 to 2019. (_+)\1+¶ $1 $& Precede each number with its highest proper ...


1

C++ (clang), 288 ... 225 211 bytes #import<queue> #import<regex> #define l find(begin(q),end(q), using v=std::vector<int>;v f(int p){v q{1};for(int n=1,d,i;d=i=++n<2020;q.insert(l d),n))for(;++i<n&d<2;)d=n%i?d:n/i;auto t=l p);return{*++t,t[-2]};} Try it online! Saved 21 bytes thanks to @ceilingcat!!!! Saved 1 byte thanks to @...


0

Ruby, 104 101 95 bytes ->n{*w=1;(2..2019).map{|z|w.insert(1+w.index(w.select{|x|z%x<1}.max),z)};w[w.index(n)-1,3]-[n]} Try it online!


1

Perl 5, 88 bytes sub f{$_=1;for$n(2..2019){map{$n%$_ or$m=$_}1..$n/2;s/\b$m\b/$&,$n/}/(\d+),$_[0],(\d+)/} Try it online! Same ungolfed: sub f { $_=1; # $_ is the comma separated queue "array string" # ...with 1 as the first penguin for $n (2..2019){ # process the rest ...


6

Perl 6, 79 76 71 bytes {($!=sort {[R,] -$_,{first $_%%*,$_^..1}...1},^2020)[+(@$!...$_)X-2,0]} Try it online! Sorts numbers 1 to 2019 lexicographically by the reversed, negated sequence of largest divisors, then finds the neighbors. Example mappings: 1760 => (-1 -11 -55 -110 -220 -440 -880 -1760) 33 => (-1 -11 -33) 99 => (-1 -11 -33 -99) In ...


0

Python 3, 97 bytes lambda x,n=1:[n>x*x,x+n][t(x+n)==t(x)]or s(x,n+1) t=lambda n:sum(k//n*k%n>n-2for k in range(n*n)) Try it online! Totient function taken from Chas Brown's answer, originally from Dennis.Returns True for cases where M doesn't exist, though if that doesn't satisfy you, a far less efficient version that returns False is only two bytes ...


3

05AB1E, 16 15 bytes Ž7ëLΣÒR(}ûI¡€θ¨ Port of @JonathanAllan's Jelly answer, so make sure to upvote him! -1 byte thanks to @Grimmy. Outputs in the order [front, behind]. Try it online or verify all test cases. Explanation: Ž7ë # Push compressed integer 2019 L # Pop and push a list in the range [1,2019] Σ # Sort ...


4

Jelly, 15 bytes ⁽¥ØÆfṚNƊÞṣị"@Ø. A monadic Link accepting an integer between 1 and 2019 * which yields a list of two integers, [in-front, behind]. * If 1 or 1024 is input the missing ticket number will be shown as 0. Try it online! How? ⁽¥ØÆfṚNƊÞṣị"@Ø. - Link: n ⁽¥Ø - literal 2019 Þ - sort (range [1..2019]) by: Ɗ ...


2

Python 2, 137 133 121 bytes q=[1] for n in range(2,2020):j=q.index(max(m for m in q if n%m<1))+1;q=q[:j]+[n]+q[j:] print q[q.index(input())-1::2][:2] Try it online! 4 bytes thx to Arnauld; 11 bytes thx to FlipTack. Another naive implementation.


0

Jelly, 23 21 bytes ḍƇṀ=⁸k⁸j ⁽¥ØRç/ṣ⁸ṪḢƭ€ Try it online! Naïve answer that simply generates the list of penguins and finds the relevant place. A pair of links which, when called as a monad, takes an integer as its argument and returns a pair of integers indicating the penguins before and after in the list of penguins.


3

JavaScript (ES6),  105 ... 101  100 bytes A straightforward implementation that builds the full queue and looks for the penguin with the input ticket within it. Returns [front, behind]. k=>[eval("for(n=q=[];m=++n%2020;q.splice(q.indexOf(m),0,n))while(n%--m)q")[i=q.indexOf(k)+1],q[i-2]] Try it online! Commented The code in eval() builds and ...


0

Japt, 21 19 bytes ôU ÅæÈo èjX ¥Uo èjU Try it ôU range [input ...input + input] Å cut off first element æÈ return the first element to return a truty value when passed to: o range [0... next element] è number of elements that are.. jX ...


0

Ruby, 70 68 bytes ->n{(n+1..n*n).find{|m|g=->g{(1..g).sum{|h|1/h.gcd(g)}};g[m]==g[n]}} Try it online! Returns nil if not found.


5

05AB1E, 11 10 9 7 bytes L+.Δ‚ÕË -1 byte with help from @ExpiredData. -2 bytes thanks to @Grimmy. Outputs -1 if no \$m\$ exists. Try it online or verify all test cases. Explanation: L # Push a list in the range [1, (implicit) input-integer n] + # Add the (implicit) input-integer n to each to make the range [n+1, 2n] .Δ # Get the ...


0

J, 42 31 bytes (1{]) ::0[([+[:I.{=}.)5 p:i.@*: Try it online! Returns 0 if no \$M\$ exists. Explanation : (of the previous version) The argument is n @*: find n^2 and i. make a list 0..n^2-1 + to each number in the list add ]...


4

Gaia, 11 bytes :sUt¦ṇ=∆:+¿ Try it online! :s | push n,n^2 U | push range [n, n + 1, ..., n^2] t¦ | calculate [phi(n),phi(n+1), ..., phi(n^2)] ṇ | push [phi(n+1), phi(n+2), ..., phi(n^2)], phi(n) =∆ | find 1-based index of first in the list equal to phi(n), returning 0 if none : | dup the index +¿&#...


3

Python 2, 115 bytes lambda N:next((j for j in range(N+1,max(6,t(N)**2))if t(j)==t(N)),0) t=lambda n:sum(k/n*k%n>n-2for k in range(n*n)) Try it online! Returns 0 for falsey. The totient function t is based on Dennis's answer to a previous question. Times out on TIO for N=62.


1

Python 3, 160 121 bytes Saved 39 bytes thanks to @JoKing! Returns None if no \$M\$ exists: import math t=lambda n:sum(math.gcd(i,n)<2for i in range(n)) def s(x): n=x while n<x*x: n+=1 if t(n)==t(x):return n Try it online! If throwing an exception is allowed when no \$M\$ exists: Python 3, 145 114 bytes Saved 31 bytes thanks to @JoKing! ...


1

Japt -mx, 2 bytes jN Try it here


7

Jelly,  11  10 bytes r²ÆṪẹḢ$+⁸Ḣ A monadic Link accepting a positive integer which yields a non-negative integer (0 if no \$M\$ exists). Try it online! How? r²ÆṪẹḢ$+⁸Ḣ - Link: integer, n ² - (n) squared r - (n) inclusive range (n²) ÆṪ - Euler totient (vectorises) $ - last two links as a monad: Ḣ - ...


6

Perl 6, 57 52 bytes -5 bytes using the .& operator thanks to Jo King {first *.&($!={grep 1,($_ Xgcd^$_)})==.$!,$_^..$_²} Try it online! Returns Nil if no solution was found. Explanation { } # Anonymous block $_ Xgcd^$_ # gcds of m and 0..m-1 grep 1, ...


10

JavaScript (ES6),  83 ... 76  74 bytes Returns true if \$M\$ does not exist. Derived from this answer by xnor. f=(n,q,P=(n,d=n)=>p=--d&&P(n,d)+1-P(n%d?1:d))=>P(n)^q?p>q*q||f(n+1,q||p):n Try it online! How? Computing \$\phi(n)\$ This is based on the formula: $$\sum_{d|n}\phi(d)=n$$ which implies: $$\phi(n)=n-\sum_{d|n,d<...


5

Jelly, 13 12 bytes r²ÆṪ=€Ḣ$T+⁸Ḣ Try it online! A monadic link taking an integer and returning the next integer with shared totient or zero. Explanation Main link (takes integer argument z) r² | Range from z to z ** 2 inclusive ÆṪ | Totient function of each $ | Following as a monad =€Ḣ | - Check whether each ...


1

Javascript, 47 bytes d=(n,f=1,s='')=>n==f?s+n:d(n,f+1,n%f?s:s+f+',')


0

Pyth, 17 bytes AQf!%-F^RHTG^StG3 Try it online! Displays a list of triplets, each as \$(z, x, y)\$. AQ # Q = eval(input()), G = Q[0] (=n), H = Q[1] (=p) ^StG3 # Get the following: range(1, n) ^ 3 -> List of (z,x,y) tuples f # filter that on the following condition: lambda T: ^RHT # map(lambda ...


1

Poetic, 190 bytes although i agree i,a person,i was dismissive about a hell,i am not anarchic i repeat:i know theres never a hell i am doomed to actually,it seems a bit of a fraud,a lie,if He gives a man he|l Try it online! Input is in decimal. Output is an Unexpected EOF error for even inputs, and a Mismatched IF/EIF error for odd inputs. It takes ...


2

Shakespeare Programming Language, 137 bytes Try it online! #.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]Ajax:Listen tothy.You is the remainder of the quotient betweenyou a big cat.Open heart Fairly simple, just outputs the input mod 2. Maybe improvable.


0

MarioLANG, 37 bytes ;>-)+([!) ="=====#: )![(-)-< :#=====" Try it online! Outputs 1 for odd and 0 for even.


1

Commodore BASIC (C64/128, VIC-20, TheC64/Mini, C16/+4) - 21 tokenized bytes In this one, odd numbers produce a TRUE value and even numbers produce a FALSE (though on Commodore BASIC, FALSE is zero and TRUE is negative one; it does not do this, more conforms to modern-days TRUE/FALSE integer equivalents) 0 inputa:ifatH?a":"aaN1 Expanded and non-minimised ...


1

RPL, 4 bytes 2MOD Plain answer in vanilla RPL, for posterity and completeness of this thread. As often with RPL, one byte is saved in the source code by removing a space between a number and a command. I assume this is rarely observed in Forth.


Top 50 recent answers are included