| Bytes | Lang | Time | Link |
|---|---|---|---|
| 023 | APLNARS | 250112T110633Z | Rosario |
| 019 | APL Dyalog | 180101T141914Z | Uriel |
| 010 | 05AB1E | 180104T102747Z | Emigna |
| 055 | R | 180102T235755Z | Giuseppe |
| 095 | Ruby | 180102T234227Z | Reinstat |
| 101 | JavaScript Node.js | 180102T125040Z | l4m2 |
| 128 | JavaScript Node.js | 180102T121342Z | Shieru A |
| 084 | Haskell | 180101T235053Z | ბიმო |
| 010 | Pyth | 180101T184430Z | Mr. Xcod |
| 009 | Jelly | 180101T133802Z | user2027 |
| 093 | Python 2 | 180101T141219Z | Mr. Xcod |
| 104 | Python 2 | 180101T134846Z | ovs |
| 090 | Haskell | 180101T161748Z | totallyh |
| 045 | Wolfram Language Mathematica | 180101T133709Z | Martin E |
APL(NARS), 23 chars
{0π⍵:0⋄2=+/⍵=∊!⍨∘⍳⍨¨⍳⍵}
It seems that !⍨∘⍳⍨¨⍳ print one partial Pascal triangle with some 1 in less... Example
!⍨∘⍳⍨¨⍳5
┌5─────────────────────────────────────────────┐
│┌1─┐ ┌2───┐ ┌3─────┐ ┌4───────┐ ┌5───────────┐│
││ 1│ │ 2 1│ │ 3 3 1│ │ 4 6 4 1│ │ 5 10 10 5 1││
│└~─┘ └~───┘ └~─────┘ └~───────┘ └~───────────┘2
└∊─────────────────────────────────────────────┘
than flat that array and count how many number input it find there. If that number is 2 and the number of input is not a prime, return 1 else return 0.
Test:
{0π⍵:0⋄2=+/⍵=∊!⍨∘⍳⍨¨⍳⍵}¨4 8 9 12 16 18
1 1 1 1 1 1
{0π⍵:0⋄2=+/⍵=∊!⍨∘⍳⍨¨⍳⍵}¨1 2 3 5 6 7
0 0 0 0 0 0
APL (Dyalog), 44 34 24 19 bytes
5 bytes saved thanks to @Cowsquack
(~0∊⊢|⍨2↓⍳)⍱⊢∊⍳∘.!⍳
How?
We make sure that neither does
⍳ - range 0 .. n-1,
⍳∘.! - upon cartesian binomial with self
⊢∊ - contain n,
⍱ - nor does
⊢|⍨ - n modulo each item of
2↓⍳ - range 2 .. n-1
~0∊ - not contain 0 (a.k.a non-divisible)
05AB1E, 10 bytes
ÝDδcI¢IpÌQ
Explanation
Ý # push range [0 ... input]
D # duplicate
δc # double vectorized command binomial
I¢ # count occurrences of input
Ip # check input for primality
Ì # add 2
Q # compare for equality
Checks that n occurs exactly twice in the first n+1 rows of pascals triangle and isn't prime.
The comparison works as there are no primes that can occur 3 times in the triangle.
R, 55 bytes
function(n)sum(!n%%1:n)>2&!n%in%outer(1:n-1,1:n,choose)
sum(!n%%1:n)>2 is the composite test and outer(1:n-1,1:n,choose) computes rows 0 to n-1 of Pascal's triangle, so we make sure n does not appear there.
Ruby, 97 95 bytes
->n{c=!r=[1,1]
(2...n).map{|i|c|=1>n%i
[n]&r=[0,*r,0].each_cons(2).map{|a,b|a+b}}&[[n]]==[]&&c}
Scraped a couple bytes off.
JavaScript (Node.js), 103 101 bytes
n=>(r=x=>[...Array(n).keys(F=n=>n>0?n*F(n-1):1)].every(x))(i=>r(j=>F(i)/F(j)/F(i-j)-n))>r(i=>i<2|n%i)
JavaScript (Node.js), 79 133 130 128 bytes
f=(n,a=[1])=>n<a.length||[...'0'.repeat(n)].filter((x,i)=>n%i<1).length>1&&a.indexOf(n)<0&&f(n,[...a.map((x,i)=>x+a[i-1]||1),1])
evil prime checker +50 bytes :(
Haskell, 86 84 bytes
p=[]:[zipWith(+)(1:x)x++[1]|x<-p]
f n=all((>0).rem n)[2..n-1]==any(elem n)(take n p)
Explanation
The function p recursively defines a degenerate Pascal's triangle:
[]
[1]
[2,1]
[3,3,1]
[4,6,4,1]
[5,10,10,5,1]
As we can see (in this solution 1 is somewhat special) every number n appears exactly twice in the n+1th row and all elements of the subsequent rows only get bigger, thus we only need to check if n is somewhere up to the nth row to see if an element is disqualified:
any(elem n)(take(n-1)p)
Now we have True for all elements that appear more than twice (except 1), so all we need is to have a faulty isPrime function that returns True for 1:
all((>0).rem n)[2..n-1]
Jelly, 11 10 9 bytes
Thanks to:
- Martin Ender for -1 byte! (another approach, use
’(+1) avoid usingn2(-2), so -1 overall. - Jonathan Allan for bug fix.
- Dennis for another -1 byte.
Ḷc€ḶFċ=ÆP
Alternative approach, by Jonathan Allan. (flawed)
----------- Explanation -----------
Ḷ Lowered range. [0, 1, ..., n-1]
c€Ḷ `c`ombination `€`ach with `Ḷ`owered range [0...n-1]
F Flatten.
ċ Count the number of occurences of (n) in the list.
ÆP Returns 1 if (n) is a prime, 0 otherwise.
= Check equality.
Explanation for the last line:
- As pointed out by Martin Ender, "
nappears twice in the Pascal triangle" is equivalent to "ndoesn't appear in the firstn-1rows". - We want to return
trueif the number is not a prime (that is,ÆP == 0) and the countcis zero. From that we can deduce thatÆP == c.
It can be proven that if they are equal, then they are equal to 0, because:ÆPreturn a boolean value, which can only be 0 or 1.- If it returns 1, then
nis a prime, therefore it cannot appear in the firstn-1lines (that is,c == 0)
Python 2, 93 bytes
def f(n):l=[1];exec"(n in l)>=any(n%k<1for k in range(2,n))>q;l=map(sum,zip([0]+l,l+[0]));"*n
This is a named function f, which outputs via exit code, 0 for Pascal Primes, 1 otherwise.
How this works?
def f(n):l=[1]; # Define a function f (arg. n) and a list l = [1].
exec"..."*n # Execute n times.
(n in l) # Boolean: "n is in l?" is greater than...
>=any(n%k<1for k in range(2,n)) # the boolean: "Is n composite?"?
>q; # If the boolean comparison returns a falsy
# result, then continue on without any difference.
# Otherwise, evaluate the other part of the
# inequality, thus performing "is greater than q".
# Since we have no variable "q", this terminates
# with exit code 1, throwing a "NameError".
l=map(sum,zip([0]+l,l+[0])); # Generate the next row in Pascal's triangle,
# By zipping l prepended with a 0 with l appended
# with a 0 and mapping sum over the result.
This basically checks whether n occurs in the first n - 1 rows of Pascal's triangle or if it is prime, and throws an error if any of these two conditions are met.
Saved 1 byte thanks to ovs.
Python 2, 105 104 bytes
thanks to user202729 for -1 byte
a=q=[1];n=input();r=n<4;p=1
for i in range(2,n):q=a+map(sum,zip(q[1:],q))+a;r+=n in q;p*=n%i
print p+r<1
Haskell, 90 bytes
f n=2==sum[1|i<-[0..n],j<-[0..i],p[j+1..i]`div`p[1..i-j]==n,mod(p[1..n-1]^2)n<1]
p=product
Wolfram Language (Mathematica), 45 bytes
CompositeQ@#&&Binomial~Array~{#-1,#}~FreeQ~#&
Every composite number n appears exactly twice on row n and cannot appear afterwards. So the condition for Pascal primes is that they don't appear at all within the first n-1 rows.
As far as I can tell, this is shorter than checking that it appears exactly twice within the first n rows and being able to use !PrimeQ instead.