New answers tagged

2

APL, 9 chars/bytes 2≡≢∪∨∘⍳⍨⎕ get the input ⎕ compute least common divisor between n and all integers from 1 to n ∨∘⍳⍨ count of unique values ≢∪ if it's exactly 2, then it's prime 2≡


0

Symbolic Raku, 17 bytes $_=?($_%%[^] ^$_) Try it online! Suprisingly, this is shorter than the my previous Raku answer, though one byte longer than using the built-in Explanation: $_= # Set the output to ?( ) # The boolean of $_ # If the input is %% # Divisible by [^] # Only one ...


0

tq, 10 bytes ?-1!xp*p%? Explanation Uses Wilson's theorem to do the prime-checking. ?-1! # Generate all numbers from 2 to input - 1 x, # Preserve this value, prevent it from being printed p*p # Generate all possible combinations of those numbers %? # Does the product of any combination equal to the input?


0

Spice, 93 bytes ;i;m;t;c;r@REA i;ADD 0 1 c;SUB i 1 m;ADD c 1 c;MOD i c t;SWI t 1 9;SWI c m 3;ADD 0 1 r;OUT r; This outputs [1] for a prime, and [] for a non-prime. Un-golfed explanation: ;i;m;t;c;r@ - Declare vars (@ marks end of declarations) (line 0) REA i; - Read input into i (line 1) ADD 0 1 c; - Set counter c = 1 (line 2) SUB i 1 m; - Set ...


0

Python 3, 12 bytes lambda n:n&1 Outputs 1 for odd, 0 for even.


0

APL(NARS), 47 chars, 94 bytes {(m⍵[1])×n⍵[2]}{+/⍺⍺¨{k←⍳⍵⋄(⍵÷b),¨b←k/⍨0=k∣⍵}⍵} where m and n are the function one has to use (this because i don't know how to call one array of function in one function in APL). Using the example above the multiplication of Mobius function (here it is 12π) and sum of divisors function (here it is 11π) for value 12 that ...


2

JavaScript (ES6),  77 76 70  69 bytes a=>a.map((_,n)=>a[~n]=a.reduce(p=>p-=a[n/++d-1]*a[-d]|0,d=!n++)/a[0]) Try it online!


2

Charcoal, 33 bytes FLθ⊞υ∕∨‹ι²±ΣEυ∧∧λ¬﹪ιλקθ÷ιλκ§θ¹Iυ Try it online! Link is to verbose version of code. 0-indexed; on input, the 0th element is a dummy, while on output, it's a duplicate. Explanation: FLθ Loop over the indices of the input list. ⊞υ Save the results as we go. ∕∨...§θ¹ Each term is divided by \$f(1)\$. ‹ι² For the 0th and 1st terms, ...


2

Jelly, 30 29 bytes ÆD,U$ị"PṖS×Ḣḣ1N;"@ JḊç@ƒİḢ,ƊḢ Try it online! I’m sure there’s a golfier way of doing this. A pair of links which is called as a monad with the list of integers as the argument. Returns a list of integers. Explanation Helper link Takes n as its left argument and the current pair of outputs for g and f as its right argument ÆD ...


4

MATL, 32 bytes 1i1)/Gd"GX@QtZ\3L)/)y7M)*s_G1)/h Try it online! Or verify all test cases. How it works 1 % Push 1 i % Take input: f, of size L 1) % Get its first entry: f(1) / % Divide: gives 1/f(1). This is g(1). It will later be expanded into % a vector to include g(2), g(3), ... g(L) G % Push input again d ...


1

W d, 5 bytes (Possibly the only <=5-byter that does not use a prime or number factorization built-in?) ⊂ℝ]Aא Unpacked: m!Wk2= And, just for the lols (explanation for this program is also below, without the implicitly provided ba): W d, 13 7 bytes W doesn't have a boring primalty test operation, which is why this program is so long. (IMO a 7-byte ...


0

Wren, 48 43 bytes I feel surprised that I think better when using W instead of Wren. Also, the 1-primalty problem is fixed. Fn.new{|i|(1..i).where{|x|i%x==0}.count==2} Try it online!


2

APL (Dyalog Classic), 8 bytes +\⍣2∘⍳⍳⊢ My first time using APL, thanks to H.PWiz for shortening it into a train Essentially a port of the Jelly answer Explanation +\⍣2∘⍳⍳⊢ ∘⍳ Numbers from 1 to input inclusive +\ Cumulative reduce with addition, gets triangular numbers ⍣2 Repeat (does it again) to get the tetrahedral numbers ⍳⊢ ...


Top 50 recent answers are included