New answers tagged

0

Pyth, 22 bytes L?b-1sm*.cbdcyd-btdUb1 Try it online! Defines a function that is called as y<number>, e.g. yQ. L # y=lambda b: ?b 1 # ... if b else 1 -1 # 1 - s # sum( m Ub # map(lambda d: ... , range(b)) *.cbd # ...


0

C# .NET, 260 bytes class P{static void Main(string[]a){int b=int.Parse(a[0]);int g=int.Parse(a[1]);int q=g;int r=int.Parse(a[2]);int t=int.Parse(a[3]);int w=t;while(true){if(System.Console.ReadKey().KeyChar<104)q--;else w--;if(q<1){q=g;b--;}if(w<1){w=t;r--;}if(r<1|b<1)return;}}} No TIO :( Not the shortest but does the job great!EDIT: NOTE: ...


0

Stax, 4 bytes ╩+Pî Run and debug it I omitted the final test case. The algorithm is correct, but the time requirements are beyond my patience or predicted lifespan. Method: Compute factorial Calculate "multiplicity" by 10.


0

Wolfram Language (Mathematica), 90 bytes Tr[1^Union[First/@Join@@FactorInteger/@Select[z=Range@#2^3;Join@@{#-z,#+z},Not@*PrimeQ]]]& Try it online! un-golfed: the code is read mostly from right to left, F[n_, x_] := Length[Union[ (* number of unique elements *) First /@ ...


2

Python 3, 435 bytes This challenge has been on my bucket list for a while, but it's only recently that: a) I put time and attention into actually attempting an answer; and b) actually tested my idea to calculate the size of the numbers (and so the number of primes by comparing it to the size of the primorials) using some unholy combination of logarithms and ...


0

Malbolge, 57283 bytes TryItOnline link didn't fit in the answer. What a shame! bP&A@?>=<;:9876543210/.-,+*)('&%$T"!~}|;]yxwvutslUSRQ.yx+i)J9edFb4`_^] \yxwRQ)(TSRQ]m!G0KJIyxFvDa%_@?"=<5:98765.-2+*/.-,+*)('&%$#"!~}|utyrqvu tsrqjonmPkjihgfedc\DDYAA\>>Y;;V886L5322G//D,,G))>&&A##!7~5:{y7xvuu,10/ .-,+*)('&%$#"yb}|{...


0

05AB1E, 3 bytes Hà> Try it online or validate all test cases. Explanation: # implicit input H # convert each digit to decimal à # maximum > # add 1 # implicit output


1

Reg (a.k.a Unofficial Keg), 17 bytes Try it online behavior is unknown. ¿¿ï_'(:|_)(+). This takes 2 inputs separated by newlines.


0

APL(NARS), 8 chars, 16 bytes {+/⍺..⍵} test: f←{+/⍺..⍵} 1 f 2 3 2 f 10 54 This it is 6 chars but I not find a way to assign a name to it +/↑../ Using as function 2 3


1

33, 20 bytes OcaxcmszOcaxaclaz2do Try it online! Explanation This answer uses the following formula to calculate it: $$a + (a+1) + (a+2) + \cdots + b = {a-a^2+b+b^2 \over 2}$$ O O | Get a and b as two integers delimited by whitespace ca | (a ) xcm | ( - a^2 ) sz ...


0

Stax, 4 bytes ╖c╟Y Run and debug it


1

APL(NARS), 83 chars, 166 bytes r←B w;i r←,1⋄i←0x⋄w+←1 →3×⍳w≤i+←1⋄r←r,1-+/{(1+i-⍵)÷⍨(⍵!i)×r[⍵+1]}¨0..i-1⋄→2 r←r[i] Input as integer output as big rational B 0 1 B 1 1r2 B 2 1r6 B 3 0 B 4 ¯1r30 B 10 5r66 B 100 ¯94598037819122125295227433069493721872702841533066936133385696204311395415197247711r33330 B 1000 ¯...


2

PowerShell, 28 bytes -5 = 23 ('i',-1,'-i',1)["$args"%4-1] Try it online! Port of all the cyclic indexing


0

Pyth, 17 bytes FG.Qs[Gdtl.usjNTG Try it online! If I can instead of values on different lines, take a list of numbers (in the format [9999999999, 10, 8, etc.]), then -1 byte by replacing .Q with Q F # For G.Q # G in the complete input (split by newlines) s[ # join the list as string, create list from ...


0

33, 62 bytes s'i'{1a}'d'{1m}'m'{2x}'h'{2d}'P'{o}'e'{@}It[mzsjk""ltqztItn1a] Try it online! This program takes the instructions delimited by newlines Explanation: It[mzsjk""ltqztItn1a] [mz n1a] | Forever It jk It | - Get the first character of the next instruction qz | - Call the function declared previously ...


0

05AB1E, 16 bytes 1[ÐU²FXm}¹@#5(°+ Port of @KeithRandall's Python answer. Try it online. Explanation: 1 # Push a 1 [ # Start an infinite loop: Ð # Triplicate the top value on the stack U # Pop and store one in variable `X` ²F # Inner loop the second input amount of times: ...


0

Perl 5, 254 bytes sub f{@s=map[$_>$_[0]||0,$_>$_[0]?$_[3]:$_[2],1],1..$_[0]+$_[1];while(join('',map$$_[0],@s)=~/01|10/){($c,$p)=&{$_[4]};(@w=sort{$p*($$b[1]<=>$$a[1])}grep$$_[2]&&$$_[0]eq$c,@s)[0][2]=0;@s=grep$$_[2]||++$$_[2]&&--$$_[1],@s if@w<2}sort{$b<=>$a}map$$_[1],@s} Try it online! Ungolfed but same – with ...


1

Charcoal, 22 bytes F²⊞υ×NNW∧⌊υS§≔υ℅ι⊖§υ℅ι Try it online! Link is to verbose version of code. Requires both black shirt values, then both white shirt values, then a stream of b and w characters on separate lines, otherwise behaviour is undefined. On TIO if you don't provide enough shirts you can see Charcoal attempt to prompt for input; if you ran locally ...


1

Ruby, 79 bytes ->a,b{x=y=1.0;z=a;eval"y=(x+z)/2;x,z=a<eval('y**'*~-b+?y)?[x,y]:[y,z];"*99;p y} This is the same as the below program, but less accurate since it only runs 99 loops. Ruby, 87 bytes ->a,b{x=y=1.0;z=a;(y=(x+z)/2;x,z=a<eval("y**"*~-b+?y)?[x,y]:[y,z])while y!=(x+z)/2;p y} Try it online This is simply bisection. Ungolfed: -> a,...


0

Python 2, 54 bytes b,w,B,W=input() l=[B*b,W*w] while all(l):l[input()]-=1 Try it online!


0

Perl 5, 205 bytes sub f{@s=map[$_>$_[0]?'w':'b',$_>$_[0]?$_[3]:$_[2],1],1..$_[0]+$_[1];while(join('',map$$_[0],@s)=~/bw|wb/){$c=&{$_[4]};(@w=grep$$_[2]&&$$_[0]eq$c,@s)[0][2]=0;@s=grep$$_[2]||++$$_[2]&&--$$_[1],@s if@w<2}@s} Try it online! I assumed that picking the least worn out shirt each day doesn't actually influence the ...


3

05AB1E, 14 bytes *Š*‚[ICÓ-W_iõq Try it online! Uses p instead of b, because pink is prettier than black. Explanation: * # number of white shirts * durability of white shirts Š # swap * # number of pink shirts * durability of pink shirts ‚ # pair the two products in a list [ # infinite loop I ...


1

05AB1E, 28 22 bytes *U*V[XY*_iõq}©XαUY®≠-V Inputs are separated by newlines in the order amountOfWhiteShirts, whiteDurability, amountOfBlackShirts, blackDurability, shirtDigits..., where white shirts are 1 and black shirts are 0. -3 bytes thanks to @Grimy by bringing to my attention that an input of 1/0 instead of "w"/"b" is allowed for the shirts. Try ...


1

C(tcc), 117 116 90 bytes Uses 0x01 instead of b, and 0x02 instead of w. main(a,b,c,d){scanf("%d%d%d%d",&a,&b,&c,&d);a*=c;for(b*=d;a*b;~c?b-=!c:a--)c=getchar()-2;} Try it online!


0

21. Flobnar, A010709 (All 4s) //0q ÷GxJiiiiihhZUUUUUUUNYAxcccccbCLDLxyzUUUUUTxyzJCLOzUUUUUURzyzxyzxyzcccccbbCLxGC//*/0e#§≈2*1z⌂'>[=====[===]]=[[==========]]=[ /*] 박망희 0# ;*/ //\u000A\u002F\u002A n=>//\u002A\u002Fn-> /**/""+n==""+n?5/2>2?1:40-/**/n:n*n//AcaAcAAI(((1)(1)(1)1)((1)(((1)1)1)1)(((1)(1)1)(((1)((1)1)(1)1)...


0

Python 3, 50 bytes f=lambda n:0if n<10else-~f(eval('+'.join(str(n)))) Recursive function. Takes an integer, returns an integer. Explanation: # create a lambda function which takes one argument, and assign it to f f=lambda n:\ # if n is 0-9, it's persistence 0 0if n<10\ # otherwise, add one to the running total and recursively call f ...


0

Python 2, 62 bytes f=lambda x,i=0:f(`sum(int(i)for i in x)`,i+1)if len(x)>1else i


1

Stax, 8 11 bytes ªwæMε∞ö?îm⌐ Run and debug it +3 bytes thanks to @Khuldraeseth (the first answer didn't have compliant output)


0

Swift 5, 106 bytes func a(_ s:S,_ i:I)->(S,I){if s.count<=1{return(s,i)};return a(S(s.compactMap{I(S($0))}.reduce(0,+)),i+1)} Try it online!


1

K (ngn/k), 16 bytes Solution: {x,#1_(+/10\)\x} Try it online! Explanation: {x,#1_(+/10\)\x} / the solution { } / lambda taking implicit x ( )\x / iterate until convergence 10\ / split into base-10 (123 => 1 2 3) +/ / sum 1_ / drop first result (iterate returns input as first result) ...


0

Julia, 92 (29) bytes f(n)=n>9&&(f∘sum∘digits)(n)+1 Edit: With correct printing it's 92: f(n)=n>9&&(f∘sum∘digits)(n)+1 while(n=parse(Int128,readline()))≢π println("$n ",f(n)%Int)end


1

MathGolf, 11 bytes hÅ_Σ]▀£(k ? Try it online! Incredibly inefficient, but we don't care about that. Basically, using the fact that the additive persistence of a number is smaller than or equal to the number itself. Uses the fact that the additive persistence is less than or equal to the number of digits of the number. Passes all test cases with ease now. ...


0

Python 3, 76 bytes def f(n,i=0,p=0):p=p or n;f(sum(map(int,str(n))),i+1,p)if n>9else print(p,i) Try it online! Without I/O restrictions: Python 3, 54 bytes f=lambda n,i=0:f(sum(map(int,str(n))),i+1)if n>9else i Try it online!


1

05AB1E, 13 bytes ε.µΔSO¼}¾}<ø» Input as a list of integers. Try it online. Explanation: ε # Map each integer in the (implicit) input to: .µ # Reset the counter variable to 0 Δ # Loop until the integer no longer changes: S # Convert it to a list of digits O # And take the sum of those ¼ # Increase the counter variable by ...


1

C, 50 bytes p,i;f(int n){p=1;for(i=1;p<n;p*=i++);return p==n;} Special Thanks to Jo King for shortening the program.


-1

C, 311 bytes r(int*s,int*o,int n){int h=1;while(h<=n){if(h==2){printf("%d %d\n",1,1);o[0]=1;o[1]=1;}else if(h==1){printf("%d\n",1);o[0]=1;}else{int j=0;int*s_p=s,*o_p=o;*o_p++=1;printf("1 ");while(j<(h-2)){*o_p+=*s_p++;*o_p+++=*s_p;printf("%d ",*(o_p-1));j++;}*o_p=1;printf("1\n");}memcpy(s,o,100);memset(o,0,100);h++;}} The complete, readable version ...


0

Pyth, 6 bytes m.cLdh Try it online! Code |Explanation --------+------------------------------ m.cLdh |Full code m.cLdhdQ|With implicit variables filled --------+------------------------------ m Q|For each d in [0, input): L hd | For each k in [0, d]: .c d | dCk L | Collect results in a list m |Collect results in a list |...


1

C, 59 bytes s,a,n=1e3;C(m){for(a=m;m*n>a;s++)m>n?m-=n:(n-=m);return s;} Try It online The input is an integer which is the width / height ratio in thousandths (eg. 1002 for 1.002:1). Ungolfed version int C(int m) { int n = 1000; int a = m; int s = 0; while (m * n > a) { if (m > n) m -= n; else ...


1

APL(NARS), 26 chars, 52 bytes {k(4×⍵),2+k←¯1+×⍨2×⍵}¨⍳100 test: a←{k(4×⍵),2+k←¯1+×⍨2×⍵}¨⍳100 ≢a 100 +/{(x y z)←⍵⋄(z*2)=(y*2)+x*2}¨a 100 +/{∨/⍵}¨a 100 10↑a 3 4 5 15 8 17 35 12 37 63 16 65 99 20 101 143 24 145 195 28 197 255 32 257 323 36 325 399 40 401


0

Scala, 83 bytes def^(n:Long):Long={println(n);if(n>9)^((n+"").map(x=>(x-48).toLong).product)else n} Try it online!


2

Chevron, 100 bytes >^__>>^n ->+11?^n<10 >^n ^d<<1 ^i<<1 ^m<^i>^n ->+4??^m=^__ ^d<<^d*^m ^i<<^i+1 ->-4 ^n<<^d ->-10 ><^n This is a fairly new language of my own creation - prototype interpreter, documentation, and example programs can be found at https://github.com/superloach/chevron. ...


1

Clojure, 73 bytes #(loop[i %](prn i)(if(> i 9)(recur(apply *(for[c(str i)](-(int c)48))))))


1

APL(NARS), 111 chars, 222 bytes r←f w;A;a;P;Q;m m←⎕ct⋄Q←1⋄⎕ct←P←0⋄r←,a←A←⌊√w⋄→Z×⍳w=0 L: →Z×⍳0=Q←Q÷⍨w-P×P←P-⍨a×Q⋄r←r,a←⌊Q÷⍨A+P⋄→L×⍳Q>1 Z: ⎕ct←m The f function is based on the algo one find in the page http://mathworld.wolfram.com/PellEquation.html for solve Pell equation. That f function has its input all not negative number (type fraction too). ...


1

APL(NARS), 107 chars, 214 bytes dd←{⍵=0:0⋄0>k←1∧÷⍵:-k⋄k}⋄nn←{1∧⍵}⋄f←{⍵=0:0x⋄m←⎕ct⋄⎕ct←0⋄z←{0=(d←dd⍵)∣n←nn⍵:n÷d⋄r←⌊n÷d⋄r,∇d÷n-r×d}⍵⋄⎕ct←m⋄z} The function of the exercise f, should have as input one rational number (where 23r33 means as "23/33" in math) ...The algo is the same of the one Neil posted as JavaScript solution... test f 860438r1 860438 ...


0

20. Neim, A008592 (10*n) //0q ÷GxJiiiiihhZUUUUUUUNYAxcccccbCLDLxyzUUUUUTxyzJCLOzUUUUUURzyzxyzxyzcccccbbCLxGC//*/0e#§≈2*1z⌂'>[=====[===]]=[[==========]]=[ /*] 박망희 0# ;*/ //\u000A\u002F\u002A n=>//\u002A\u002Fn-> /**/""+n==""+n?5/2>2?1:40-/**/n:n*n//AcaAcAAI(((1)(1)(1)1)((1)(((1)1)1)1)(((1)(1)1)(((1)((1)1)(1)1)1)(((...


2

Python 2, 49 bytes a=input()**.5 x=2 while x%a*x>1:x+=1 print x,x//a Try it online! Finds x as the smallest number above 1 where x % sqrt(n) <= 1/x. Then, finds y from x as y = floor(x / sqrt(n)).


1

Perl 6, 65 bytes {my \d=($^b-$^a)/$^n;sum ($a,*+d...*)[($^k+^0>d)+ ^$n]».&^f X*d} Try it online! Relatively straightforward. The only complication is handling the a > b case, which I do by xor-ing the input flag $^k with 0 > d, which inverts it when a > b.


0

Python 2, 64 bytes f=lambda n,x=2,y=1:x*x-n*y*y-1and f(n,x+(x==y),y*(y<x)+1)or(x,y) Try it online! Returns (x, y).


2

Python 2, 99 94 bytes A bit of a naive solution. def R(f,a,b,n,k):s=cmp(b,a);d=s*(b-a)/n;return s*sum(d*f([0,a,b][s]+i*d)for i in range(k,n+k)) Try it online!


1

Stax, 4 bytes ╣╩┼► Run and debug it As small as it is, it's not a built-in. The built-in rationals help quite a bit though. Unpacked to ascii, the program is rksu+. Reverse the array. Fold the array using (a, b) => (a + 1/b).


Top 50 recent answers are included