New answers tagged

0

FEU, 21 bytes u/x m/^(xx)+x$/1/x+/0 Try it online! 0 for even, 1 for odd


0

SystemVerilog, 29 bytes task t(n);$write(n%2);endtask Prints 1 for odd numbers and 0 for even numbers. Testbench: module m; initial begin t(1); t(2); t(16384); t(99999999); end task t(n);$write(n%2);endtask endmodule Output (VCS on EDA Playground): 1 0 0 1


0

Microsoft Word Math Autocorrect, 40 bytes Add the following entries to your Math Autocorrect table: 0$ -> $0 1$ -> $1 2$ -> $0 3$ -> $1 4$ -> $0 5$ -> $1 6$ -> $0 7$ -> $1 8$ -> $0 9$ -> $1 Then make a new equation. Separate each digit with a space, or this will not work. When you type your last digit, it will be replaced by ...


1

Pyramid Scheme, 108 92 bytes ^ /-\ ^---^ -^ ^- -^- /^\ ^---^ -^ /#\ /-\---^ / 1 \ /l\ -----/ine\ ----- Try it online! Outputs -2 for odd numbers and 0 for even ones. This can be swapped by switching the top pyramid for + instead of -. This is basically equivalent to (-1)**int(input) - (-1)**0


1

Brainetry -w 2, 25 bytes Golfed version: a b c d e f a b c d e f g How it works: we set the cell size to 2 with -w 2 which pretty much does all the """heavy""" lifting for us. After that we just input one number and output it. If you want to input an actual number instead of an ASCII code point you can also set the --numeric-io ...


0

APL (Dyalog Extended), 26 bytes (×/≡⍥(+/⍣≡10⊤⊢)⊢×1<≢)⍭⍤⌈∘2 Try it online! A golfed version of Sherlock9's solution. A nice use case for Over f⍥g, which performs g on both sides and combines the two with f. How it works (×/≡⍥(+/⍣≡10⊤⊢)⊢×1<≢)⍭⍤⌈∘2 ⍝ Input: n ⌈∘2 ⍝ max(n,2) ⍭⍤ ⍝ Prime factorization of ...


0

Rutger, 504 bytes x=$Input; t=Not[IsPrime[$x]]; c=(); d=p=0; i=IfElse[{l=LessThan[$x];l[1]}]; i=i[{t=0;}]; i=i[{s=Array[Str[$x]];e=Each[$s];e=e[@i];e=e[{a=Add[$d];d=a[Integer[$i]];}];Do[$e];r=2;w=While[{Decrement[$x];}];w=w[{i=IfElse[{m=Modulo[$x];Not[m[$r]];}];i=i[{a=Append[$c];v=Divide[$x];x=Integer[v[$r]];c=a[Array[Str[$r]]];}];i=i[{r=Increment[$r];}];Do[$...


0

Whispers v2, 63 bytes > Input >> 1² >> [2] >> φ(L) >> Each 4 3 >> 1∈5 >> Output 6 Try it online! Works by brute force. We square the input, then generate the range [1, 2, ..., x²]. Next we calculate \$\phi(i)\$ for each \$i\$ in this range. Finally, we check if the original input \$x\$ is in the array of \$\phi(i)\$ ...


1

Io, 57 bytes method(a,b,m,x :=1;for(i,1,m,if((x=x*a%m)==b,return i))0) Try it online!


2

Haskell, 42 bytes (a#m)b=last$0:[x|x<-[1..m],mod(a^x-b)m==0] Try it online! The function (a # m) b returns a positive integer x such that a ^ x == b (mod m). If no such x exists, it returns 0. This is done by the brute force method.


1

Brachylog, 19 bytes Takes in a list of [A,M,B], output is either X or false. The [3306, 5359, 4124] test case times out on TIO, but returns the correct result locally. First Brachylog answer, so probably not the best solution. :-) bhM>.>0&h;.^;M%~t?∧ Try it online! How it works bhM>.>0&h;.^;M%~t?∧ bhM set the second item ...


2

bc, 58 50 bytes define f(a,b,m){for(;x<m;)if(a^++x%m==b)return(x)} Try it online! This just tries the integers from 1 to m, and outputs the first one which satisfies being a discrete log. Otherwise, the function returns 0 (default return value).


1

Retina, 84 bytes \d+ * "$+"{`,(?=(_+))((_+),)+ ,$.1*$3$& )`^(_+),\1+ $1, L$`.*(,_+)(,_+)+$(?<=\1) $#2 Try it online! Sadly no test suite as this uses "$+", and I can't figure out how to emulate that with multiple sets of inputs (Retina just crashes when I try). Takes input in the order m, a, b and produces no output if there is no ...


10

C (gcc), 59 53 51 bytes Saved 6 bytes thanks to the man himself Arnauld!!! Saved 2 bytes thanks Dominic van Essen!!! p;x;f(a,b,m){for(p=a,x=1;p-b&&++x<m;)p=p*a%m;x%=m;} Try it online! Inputs positive integers \$a,b,m\$ with \$a,b<m\$. Outputs a positive integer \$x\$ such that \$a^x\equiv b\ (\text{mod}\ m)\$ or \$0\$ if no such \$x\$ exists.


2

Java 8, 97 bytes (a,b,m)->{for(int x=0;x++<m;)if(a.modPow(b.valueOf(x),b.valueOf(m)).equals(b))return x;return-1;} \$a\$ and \$b\$ are both java.math.BigInteger; \$m\$ and the output \$x\$ are both int. Outputs -1 if no \$x\$ is found. Try it online. Explanation: (a,b,m)->{ // Method with 2 BigInteger & integer parameters and ...


1

Charcoal, 14 bytes NθI⊕⌕﹪XN…·¹θθN Try it online! Link is to verbose version of code. Takes input in the order m, a, b and outputs 0 if there is no solution. Explanation: Nθ Store `m` …·¹θ Range from 1 to `m` inclusive XN Take powers of `a` ﹪ θ Reduce modulo `m` ⌕ N Find index of `b` ⊕ ...


1

R, 61 bytes function(a,b,m){for(i in c(1:m,0))if((T=(a*T)%%m)==b)break;i} Try it online! The base form of R doesn't support arbitrary-precision arithmetic (see my other 'R+gmp' answer for a solution using the 'gmp' library that allows this). But, pleasingly, the step-by-step calculation of (a^x)mod m comes-out at only 14 bytes longer than the brute-force ...


3

MathGolf, 8 bytes _╒k▬\%=) Port of my 05AB1E answer, so also: Inputs in the order \$m,a,b\$; outputs 0 if no \$x\$ is found. Try it online. Explanation: _ # Duplicate the first (implicit) input `m` ╒ # Pop one and push a list in the range [1, `m`] k # Push the second input `a` ▬ # For each value `x` in the list, take `a` to ...


3

R+gmp, 47 46 bytes Or only 37 bytes by requiring input in the form of bigz big integer. function(a,b,m)match(T,as.bigz(a)^(1:m)%%m==b) Try it online!


3

Ruby, 40 bytes ->a,b,m{(a**$.+=1)%m==b&&$.||$.<m&&redo} Try it online! For the exponent \$x\$, uses the predefined variable $., which is normally the number of the last line read (and so is initialised to 0). The logic is straightforward: increment $. and return it if it satisfies the required equation, otherwise repeat while $. is ...


5

05AB1E, 10 7 bytes Lm¹%³k> Inputs in the order \$m,a,b\$; outputs 0 if no \$x\$ is found. Try it online or verify all test cases. Explanation: L # Push a list of values `x` in the range [1, (implicit) input `m`] m # Take the (implicit) input `a` to the power of each of these `x` ¹% # Take each modulo the first input `m` ³k # ...


5

JavaScript (Node.js),  38  33 bytes Expects (a,m)(b) as 3 BigInts. Throws RangeError if there's no solution. (a,m,x=m)=>g=b=>a**--x%m-b?g(b):x Try it online! JavaScript (Node.js), 39 bytes Expects (a,m)(b) as 3 BigInts. Returns false if there's no solution. NB: This version always returns the smallest solution. (a,m,x=0n)=>g=b=>a**++x%m-b?x&...


3

APL (Dyalog Extended), 23 bytes {⍺(∊×⍳⍨)(⍺⍺|⍵×⊢)⌂traj⍵} Try it online! Dyalog APL can't handle large integers, so a modulo should be performed after each iteration. How it works {⍺(∊×⍳⍨)(⍺⍺|⍵×⊢)⌂traj⍵} ⍝ dop; ⍵ ⍺ ⍺⍺ ← a b m ( )⌂traj ⍝ Collect all iterations until duplicate is found ⍵ ⍝ starting from a: ⍵...


6

Python 2, 55 51 bytes def f(a,b,m,x=1):a**x%m==b<exit(x);x<m<f(a,b,m,x+1) Try it online! A recursive function that simply tests all exponents from \$1\$ to \$m\$. Returns through exit code: a positive exponent \$x\$, or \$0\$ if no such \$x\$ exists.


1

05AB1E, 10 bytes ÝãDnOsP+Iå Try it online or verify all test cases. Explanation: Ý # Push a list in the range [0, (implicit) input] ã # Create all possible pairs by taking the cartesian product on itself D # Duplicate this list of pairs n # Square each inner value in the pairs: [i²,j²] O # Sum each inner ...


1

Add++, 28 26 bytes D,g,@@,*aaz€b*Fs L,0rd‽gAe Try it online! or verify the test cases Takes a long time for large inputs. Times out on TIO for the 501 and larger test cases. How it works D,g,@@, ; Define a helper function that takes 2 arguments, i and j ; Example: i = 0, j = 2 STACK = [0 2] * ; Multiply STACK =...


4

Python 2, 166 bytes u=0x104941b82b6e51bed5 v=0x48f880d7eeb3f6caa k=2*u*v a=0x4edf512cd794532694b80d70c2648adb08931 exec"u,v=a*u+7766*k*v,a*v+609*k*u;"*1164 print u*u*v*v*48222351474/4657 Try it online! Based on Arnauld's formula and method. I start with an initial u,v, and update them by a 2*2 matrix operation 1164 times. I looked for constants ...


0

Pyth, 11 bytes L?b-byyytb0 Try it online! The program defines a lambda function. The link adds yQ for I/O


0

R, 82 69 bytes s=t(expand.grid(rep(list(c(-1,1)),n<-scan())))*1:n;t(s[,!colSums(s)]) Try it online! Edit: -13 bytes by using built-in expand.grid function to automatically create matrix of all combinations of +1 and -1. Selects rows of matrix of 1:n in all combinations of positive & negative which sum to zero. Commented version: s=t( ...


7

JavaScript (Node.js),  373 ... 304  296 bytes Returns a BigInt of 206545 digits. Most BigInt literals in the code are stored as strings in base 119. This saves 8 bytes but leads to many unprintable characters. Below is a sanitized version without this compression scheme. _=>[...1e9+[9542]].map(i=>M.push(m=([a,b,c]=m,[d,e,f]=M[i-2]||m,[v=a*d+b*f,a*e+b*...


1

Charcoal, 74 bytes NθFθFιFκF⁼X⊕ι²ΣX⊕⟦κλ⟧²⊞υ⊕⟦ικλ⟧≔⁰ηW¬ⅉ«≔Eυ§κ÷ηX³λζ≦⊕η≔Xζ²ε¿¬⊙ε⊙ε№ε⁺κμI⁻Eθ⊕κζ Try it online! Well, for n<50, otherwise it gets too slow. Link is to verbose version of code. Based on @JonathanAllen's answer. Explanation: Nθ Input n. FθFιFκ Loop through all potential Pythagorean triples. F⁼X⊕ι²ΣX⊕⟦κλ⟧² If this is indeed a triple, ⊞υ⊕⟦ικλ⟧...


10

Wolfram Language (Mathematica), 84 83 bytes 224571490814418y^2/.{1}.FindInstance[x^2-410286423278424y^2==1&&x>1,{x,y},Integers] Try it online! -1 byte thanks to @J42161217. Gives the identical result to the existing Sledgehammer solution. Uses the Pell equation directly to find the required y, and substitutes into the formula for the desired ...


12

Sledgehammer, 67 66 26 bytes -40 bytes thanks to @GregMartin and I also no longer have any idea about how my answer works Completes in less than a few seconds! ⡇⣄⠀⠇⣺⠇⢞⡞⣵⣍⠪⢺⡇⠜⢂⡒⢃⠦⠲⣎⠇⠣⡔⢻⡦⠔ Mathematica code: Floor[Divide[25194541,184119152] * (NumberFieldFundamentalUnits@Sqrt[4729494])^4658] (the reason for the Divide is that by default Mathematica represents ...


0

T-SQL, 152 bytes Input is a varchar(max) Output format is +1+2-3 WITH C as(SELECT @*1d,@+null o,0f UNION ALL SELECT~-d,concat(char(44+s),d,o),f+s*d FROM C,(values(-1),(1))x(s)WHERE d>0)SELECT o FROM C WHERE f=0and d=0 Try it online


2

R, 99 95 bytes n=scan():1 f=function(j)outer(a<-n[j]^2,a,`+`)%in%a while(any(f(i<-sample(!0:1,n,T)),f(!i)))0 i Try it online! Outputs a vector of TRUE and FALSE representing in reverse order which set each integer belongs to. (The footer of the TIO transforms this into a list of integers in the first set.) Works by random sampling: repeatedly draw a ...


3

05AB1E, 14 bytes Port of the 17 byte Jelly answer. (Læ3ùʒDnO;tå}€н is the same length) Læ3ùʒnRćsOQ}€н Try it online! Explanation L Length range æ Powerset 3ù Pick truples (length-3 tuples) ʒ Filter: n Square all items R Reverse the list ć Head-extract (head ...


6

J, 37 bytes Brute forces through the possible sets, outputs the bit mask. ((-&.#.+./@,)[(e.~+/~)/.*:@#\)^:_@#&1 Try it online! (Also outputs list as numbers for easier comparison.) How it works ((-&.#.+./@,)[(e.~+/~)/.*:@#\)^:_@#&1 #&1 convert to list of N 1's ( )^:_ do ...


2

Wolfram Language (Mathematica), 1664 bytes works for all n (1 to 7824) instantly IntegerDigits[Uncompress@"1:eJwllsmRHDkMRXWVGfIAKwGYoKtMGAfG/5veT0V0dHVlkiDwN/av//7/87t+/vjhPfem6tm+K6+yqFft5e/e9fXzeH6W62050307NdsTzYtIe/mMApOd/mJY5G/Yn9FdxidPe95uu/lGRNu2+U2n1c6zypfbWza7m80S99pKerF625V9oeNuzW1ywnt92jfZUcXbV94eHrael5Evyo1vHazcy3LPpOa7fXNlmW6d0xF0QXvLBHP+...


2

Jelly, 30 26 bytes œ|/L=³ Œc§œ& ŒP²ÇẸƊÐḟŒcÑƇḢ Try it online! How? This does a more brute-force approach, filtering subsets of [1..n] based on whether they contain any Pythagorean triples. Then, it finds two triple-less subsets that have all n elements between them œ|/L=³ # Test if a pair of sets unions to [1..n] œ|/ # Set ...


5

Jelly, 18 bytes œc3²SHeƊ$Ƈ ÇŒpÇÞḢQ Try it online! (too inefficient for \$n>25\$ on TIO). How? Strategy: Find all Pythagorean triples using \$[1,n]\$ then find a way to pick 1 element from each of them such that the resulting set contains no Pythagorean triples. That way we have a set which both contains no Pythagorean triple and blocks the other set from ...


4

Wolfram Language (Mathematica), 132 116 bytes {1}.SatisfiabilityInstances[And@@(And[Or@@#,Nand@@#]&/@Map[x,Select[#~Tuples~3,{1,1,-1}.#^2==0&],{2}]),x/@#]&@*Range Try it online! This uses Mathematica's SAT solver to label the integers 1 through the input as True and False. This is composed with Range, so what feeds into the main function is a ...


4

JavaScript (ES6),  118  117 bytes Much slower for -1 byte. f=(n,a=[],b=a)=>n?f(n-1,[n,...a],b)||f(n-1,a,[n,...b]):[a,b][E='every'](o=>o[E](x=>o[E](y=>o[E](k=>k*k-x*x+y*y))))&&b Try it online! JavaScript (ES6),  122 119  118 bytes Returns one of the sets as an array. f=(n,a=[],b=a)=>[a,b][S='some'](o=>o[S](x=>o[S](y=>o[S]...


0

05AB1E, 16 bytes 0λ£Nλ₁N-Dd*åi+ë- Try it online. Explanation: λ # Create a recursive environment 0 # which starts at a(0)=0 £ # to output the first (implicit) input amount of values # (which will be output implicitly in the end) # and in each iteration, we calculate the next `a(n)` ...


0

APL (Dyalog Unicode), 40 bytesSBCS {⌽{⍵,⍨(((∊∘⍵∨≤∘0)r)×2×≢⍵)+r←(⊃-≢)⍵}⍣⍵⊢0} Try it online! I will be back for some more golfing and to write an explanation, just give me some hours. Ok I need some more time, in golfing this I managed to make it longer.


0

JavaScript (ES6), 57 bytes n=>(g=k=>n--?[p+=p<k|g[p-k]?k:-k,...g(g[p]=k+1)]:[])(p=0) Try it online!


Top 50 recent answers are included