| Bytes | Lang | Time | Link |
|---|---|---|---|
| 042 | APL Dyalog Unicode | 241022T123050Z | Ven |
| 069 | JavaScript | 241022T100958Z | Shaggy |
| 013 | Japt m | 201002T094752Z | Shaggy |
| 127 | C# | 170526T090202Z | Mormegil |
| 177 | Rockstar | 201002T094943Z | Shaggy |
| 122 | QBasic | 180919T040858Z | DLosc |
| 133 | Python 2 | 170525T144945Z | hyper-ne |
| 121 | PowerShell 3+ | 170526T123946Z | Matt |
| 079 | Haskell | 170525T231246Z | Quelklef |
| 130 | F# | 170526T124439Z | Brunner |
| 070 | Perl 6 | 170525T211810Z | Sean |
| 018 | Jelly | 170526T000328Z | fireflam |
| nan | Ruby 2.4 | 170526T001210Z | Value In |
| 091 | Python 2 | 170525T210012Z | ovs |
| 005 | 05AB1E | 170525T144736Z | Emigna |
APL (Dyalog Unicode), 42 bytesSBCS
{{1=⍴⍵:2=0+.=(⍳⍵)|⍵⋄+/⍵}¨⍵⊆⍨(1+⍺='+')/⍳≢⍺}
A chunk of this answer is prime testing.
This uses ⊆ to partition the string into boxes of either 1 or 2 numbers, then maps these numbers into either their primeness, or sums them.
JavaScript, 69 bytes
p=>a=>p.map(i=>(n=a[x++],i<a)?n+a[x++]:!(g=d=>n%--d?g(d):d-1)(n),x=0)
Japt -m, 15 13 bytes
Takes the programme as a character array, outputs true or false for the prime test.
ø²?Vv j:Vv2 x
ø²?Vv j:Vv2 x :Implicit map of each character in the first input array
ø :Contains?
² : "p"
? :If so
V : Second input array
v : Pop first element, mutating V
j : Is prime?
: :Else
Vv2 : Pop first 2 elements of V
x : reduce by addition
C#, 130 129 127 bytes
p=>d=>{var i=0;p.Any(c=>{Console.Write((c<44?d[i++]+d[i]:Enumerable.Range(2,d[i]-2).Any(x=>d[i]%x<1)?0:1)+" ");return++i<0;});}
- Saved 1 byte by currying the function (thanks to Cyoce)
- Saved 2 bytes by replacing
==with<(thanks to ceilingcat)
Rockstar, 206 197 177 bytes
Outputs 0 for composites and -0.5 for primes
listen to S
cut S
while S
listen to N
if roll S's "+"
listen to I
say N-I/-1
else
let D be N
let P be N-1
while P and D-2
let D be-1
let M be N/D
turn M up
let P be N/D-M
say P
Try it here (Code will need to be pasted in, with the programme as the first line of input and each integer on its own line)
QBasic, 122 bytes
INPUT p$
FOR i=1TO LEN(p$)
INPUT x
IF"+"=MID$(p$,i,1)THEN INPUT y:?x+y:ELSE f=0:FOR j=2TO x:f=f-(x MOD j=0):NEXT:?f=1
NEXT
Takes the code as a line of input, then takes each input number on its own line. The outputs are interspersed with the inputs because they're printed as soon as they're calculated. The truthy value is -1; falsey is 0.
Python 2, 135 133 bytes
l,p=input()
i=j=0
while len(l)-i:print(int(all(l[i]%k for k in range(2,l[i])))if p[j]=='p'else l[i]+l[i+1]);i+=1+'p+'.find(p[j]);j+=1
-2 bytes thanks to kundor
PowerShell 3+, 151 121 Bytes
$r,$v=$args;$p={0-notin((2..(($n=0+"$args")-1)|%{$n%$_}))};$i=0;$r|%{if($_-eq"p"){&p $v[$i]}else{$v[$i]+$v[($i+=1)]}$i++}
PowerShell has no prime built-ins so I had to roll my own. My first version was terrible and I took from most of the other ones that test for 0 among modulus results which saves a lot. Also sabed a few bytes by using -notin instead of -notcontains but it mean PowerShell v2 is out.
Comment based explanation
# $r is the program code. Assumed char array
# $v is the remaining variables in an assumed integer array.
$r,$v=$args
# Anonymous function to determine if a number is a prime or not.
# Test all potential factors. Check if any 0 modulus remainders are present
$p={0-notin((2..(($n=0+"$args")-1)|%{$n%$_}))}
# $i is an index for tracking location in $v
$i=0
# Cycle each of the instructions
$r|%{if($_-eq"p"){
# Call the prime checking anonymous function on this number
&p $v[$i]
}else{
# Add the next two numbers. Adjust the index accordingly.
$v[$i]+$v[($i+=1)]
}
# Next number in list.
$i++
}
# Next number in list.
$i++
}
Haskell, 88 79 bytes
('+':r)!(a:b:c)=a+b:r!c
('p':r)!(a:c)=min(foldr((*).mod a)1[2..a-1])1:r!c
_!e=e
"commands" ! [args] for use.
- Saved 9 bytes thanks to @Laikoni (#56433)
I'm still learning Haskell; golfing tips appreciated!
F#, 130 bytes
let rec r=function|[],_->()|'+'::t,f::s::u->printf"%i "(f+s);r(t,u)|_::t,n::u->printf"%b "(List.exists((%)n>>(=)0)[2..n-1]);r(t,u)
Perl 6, 70 bytes
{@^b.rotor($^a.comb.map(1+(*ne'p'))).map({$_-2??.[0].is-prime!!.sum})}
First the rotor method is used to split the input list into chunks of size 1 or 2 depending on whether the next character of the program is p or not. Then that chunked list is mapped over; chunks of size 2 are summed, and chunks of size 1 have their sole element tested for primality.
Ruby 2.4, 77+7 = 84 bytes
Uses the -rprime flag.
->g,i{g.chars.map{|c|c==?p?i.shift.prime?? 1:0: c==?+?i.shift(2).sum: p}-[p]}
Python 2, 91 bytes
p,i=input()
for c in p:n=i.pop(0);print all(n%k for k in range(2,n))if c>'+'else n+i.pop(0)
05AB1E, 5 bytes
vy.V,
Explanation
This challenge fit 05AB1E like a glove :)
vy # for each instruction in the program
.V # execute as 05AB1E code
, # print