| Bytes | Lang | Time | Link |
|---|---|---|---|
| 097 | APLNARS | 241228T163114Z | Rosario |
| 4240 | Wolfram Language Mathematica | 241219T080833Z | Introduc |
| 010 | Japt x | 180821T162102Z | Shaggy |
| 012 | MATL | 180820T211234Z | Sundar R |
| 016 | MATL | 180820T191901Z | Luis Men |
| 182 | Java 10 | 180821T092203Z | Kevin Cr |
| 009 | Brachylog | 180820T201544Z | Sundar R |
| 053 | Perl 6 | 180822T100755Z | nwellnho |
| 098 | Clean | 180821T033551Z | Οurous |
| 104 | Python 2 | 180821T181929Z | ovs |
| 095 | R | 180821T143620Z | digEmAll |
| 089 | Haskell | 180821T154149Z | ბიმო |
| 092 | JavaScript ES6 | 180820T203709Z | Arnauld |
| 041 | Physica | 180820T212617Z | Mr. Xcod |
| 008 | Husk | 180820T192429Z | ბიმო |
| 005 | Jelly | 180820T191835Z | Jonathan |
| 068 | Retina 0.8.2 | 180820T205313Z | Neil |
| 006 | 05AB1E | 180820T200216Z | Adnan |
APL(NARS), 97 chars
r←d w;i;j;p;s
r←1⋄p←⍬
r←1πr⋄p,←r⋄→2×⍳w≥r
i←r←0
→0×⍳(≢p)<j←i+←1⋄s←0
s+←p[j]⋄j+←1⋄→5×⍳s<w⋄r+←s=w⋄→4
-- 13+7+18+5+19+30+ 5=97
there is one loop for find the list of prime number <= argument w (but the last element of "p" is >w) and one loop the sum from index i, until find the w or a number >w; than increment i and begin another time until the index i is >(≢p). Below is the function with number of line used from goto →
r←d w;i;j;p;s
1: r←1⋄p←⍬
2: r←1πr⋄p,←r⋄→2×⍳w≥r
3: i←r←0
4: →0×⍳(≢p)<j←i+←1⋄s←0
5: s+←p[j]⋄j+←1⋄→5×⍳s<w⋄r+←s=w⋄→4
test:
d¨⍳10
0 1 1 0 2 0 1 1 0 1
d¨311 1151 34421
5 4 6
Wolfram Language (Mathematica), 42 40 bytes
Count[Tr/@Subsequences@Prime@Range@#,#]&
Edit: -2 Bytes thanks to att :) !
Japt -x, 17 10 bytes
There's gotta be a shorter way than this! I was right!
Craps out on the last test case.
õ fj ã@¶Xx
Try it or run all test cases (except 34421)
õ fj ã@¶Xx :Implicit input of integer U
õ :Range [1,U]
f :Filter
j : Is prime?
ã :Sub-arrays
@ :Map each X
¶ : U is equal to
Xx : X reduced by addition
:Implicit output of sum of resulting array
MATL, 15 12 bytes
EZqPYTRYsG=z
The initial E (multiply by 2) makes sure that, for prime input, the result of the later Ys (cumsum) does not have the input prime repeating itself in the zeroed part of the matrix (thus messing with the count).
Explanation:
% Implicit input, say 5
E % Double the input
Zq % Get list of primes upto (and including) that
% Stack: [2 3 5 7]
P % Reverse that list
YT % Toeplitz matrix of that
% Stack: [7 5 3 2
5 7 5 3
3 5 7 5
2 3 5 7]
R % `triu` - upper triangular portion of matrix
% Stack: [7 5 3 2
0 7 5 3
0 0 7 5
0 0 0 7]
Ys % Cumulative sum along each column
% Stack: [7 5 3 2
7 12 8 5
7 12 15 10
7 12 15 17]
G= % Compare against input - 1s where equal, 0s where not
z % Count the number of non-zeros
MATL, 16 bytes
:"GZq@:g2&Y+G=vs
Explanation
:" % Input (implicit): n. For each k in [1 2 ... n]
G % Push n
Zq % Primes up to that
@:g % Push vector of k ones
2&Y+ % Convolution, removing the edges
G= % True for entries that equal n
v % Concatenate vertically with previous results
s % Sum
% End (implicit). Display (implicit)
Java 10, 195 194 184 182 bytes
n->{var L=new java.util.Stack();int i=1,k,x,s,r=0;for(;i++<n;){for(k=1;i%++k>0;);if(k==i)L.add(i);}for(x=L.size(),i=0;i<x;)for(k=i++,s=0;k<x;r+=s==n?1:0)s+=(int)L.get(k++);return r;}
-1 byte thanks to @ceilingcat.
-10 bytes thanks to @SaraJ.
Explanation:
n->{ // Method with integer as both parameter and return-type
var L=new java.util.Stack();
// List of primes, starting empty
int i=1,k,x,s, // Temp integers
r=0; // Result-counter, starting at 0
for(;i++<n;){ // Loop `i` in the range [2, `n`]
for(k=1; // Set `k` to 1
i%++k>0;); // Inner loop which increases `k` by 1 before every iteration,
// and continues as long as `i` is not divisible by `k`
if(k==i) // If `k` is now still the same as `i`; a.k.a. if `i` is a prime:
L.add(i);} // Add the prime to the List
for(x=L.size(), // Get the amount of primes in the List
i=0;i<x;) // Loop `i` in the range [0, amount_of_primes)
for(s=0, // (Re)set the sum to 0
k=i++;k<x; // Inner loop `k` in the range [`i`, amount_of_primes)
r+=s==n? // After every iteration, if the sum is equal to the input:
1 // Increase the result-counter by 1
: // Else:
0) // Leave the result-counter the same by adding 0
s+=(int)L.get(k++);
// Add the next prime (at index `k`) to the sum
return r;} // And finally return the result-counter
It's basically similar as the Jelly or 05AB1E answers, just 190 bytes more.. XD
Here a comparison for each of the parts, added just for fun (and to see why Java is so verbose, and these golfing languages are so powerful):
- Take the input: (Jelly: 0 bytes) implicitly; (05AB1E: 0 bytes) implicitly; (Java 10: 5 bytes)
n->{} - Create a list of primes in the range
[2, n]: (Jelly: 2 bytes)ÆR; (05AB1E: 2 bytes)ÅP; (Java 10: 95 bytes)var L=new java.util.Stack();int i=1,k,x,s,r=0;for(;i++<n;){for(k=1;i%++k>0;);if(k==i)L.add(i);} - Get all continuous sub-lists: (Jelly: 1 byte)
Ẇ; (05AB1E: 1 byte)Œ; (Java 10: 55 bytes)for(x=L.size(),i=0;i<x;)for(k=i++;k<x;)and(int)L.get(k++); - Sum each sub-list: (Jelly: 1 byte)
§; (05AB1E: 1 byte)O; (Java 10: 9 bytes),sand,s=0ands+= - Count those equal to the input: (Jelly: 1 byte)
ċ; (05AB1E: 2 bytes)QO; (Java 10: 15 bytes),r=0andr+=s==n?1:0 - Output the result: (Jelly: 0 bytes) implicitly; (05AB1E: 0 bytes) implicitly; (Java 10: 9 bytes)
return r;
Brachylog, 14 9 bytes
{⟦ṗˢs+?}ᶜ
Try it online!
Multiple test cases
(-5 whole bytes, thanks to @Kroppeb!)
Explanation:
{⟦ṗˢs+?}ᶜ
{ }ᶜ Count the number of ways this predicate can succeed:
⟦ Range from 0 to input
ṗˢ Select only the prime numbers
s The list of prime numbers has a substring (contiguous subset)
+ Whose sum
? Is the input
Perl 6, 53 bytes
{+grep $_,map {|[\+] $_},[\R,] grep *.is-prime,2..$_}
Uses the triangle reduction operator twice. The last test case is too slow for TIO.
Explanation
{ } # Anonymous block
grep *.is-prime,2..$_ # List of primes up to n
[\R,] # All sublists (2) (3 2) (5 3 2) (7 5 3 2) ...
map {|[\+] $_}, # Partial sums for each, flattened
+grep $_, # Count number of occurrences
Clean, 100 98 bytes
import StdEnv,StdLib
$n=sum[1\\z<-inits[i\\i<-[2..n]|all(\j=i/j*j<i)[2..i-1]],s<-tails z|sum s==n]
Defines the function $ :: Int -> Int which works as explained below:
$ n // the function $ of n is
= sum [ // the sum of
1 // 1, for every
\\ z <- inits [ // prefix z of
i // i, for every
\\ i <- [2..n] // integer i between 2 and n
| and [ // where every
i/j*j < i // j does not divide i
\\ j <- [2..i-1] // for every j between 2 and i-1
]
]
, s <- tails z // ... and suffix s of the prefix z
| sum s == n // where the sum of the suffix is equal to n
]
(Explanation is for an older but logically identical version)
Python 2, 106 104 bytes
P=k=o=1
p=[]
i=input()
exec'o+=P%k*sum(p)==i;P*=k*k;k+=1;p+=P%k*[k];exec"p=p[i<sum(p):];"*k;'*i
print~-o
R, 95 bytes
function(x,P=2){for(i in 2:x)P=c(P,i[all(i%%P)])
for(i in 1:x){F=F+sum(cumsum(P)==x)
P[i]=0}
F}
- -24 bytes thanks to @Giuseppe who has basically revolutionized my solution supporting 34421 as well !
Haskell, 89 bytes
g n=sum[1|a<-[0..n],_<-filter(n==).scanl1(+)$drop a[p|p<-[2..n],all((>0).mod p)[2..p-1]]]
Alternative, 89 bytes
f n=length$do a<-[0..n];filter(n==).scanl1(+)$drop a[p|p<-[2..n],all((>0).mod p)[2..p-1]]
JavaScript (ES6), 92 bytes
n=>(a=[],k=1,g=s=>k>n?0:!s+g(s>0?s-(p=d=>k%--d?p(d):d<2&&a.push(k)&&k)(++k):s+a.shift()))(n)
Commented
n => ( // n = input
a = [], // a[] = array holding the list of consecutive primes
k = 1, // k = current number to test
g = s => // g = recursive function taking s = n - sum(a)
k > n ? // if k is greater than n:
0 // stop recursion
: // else:
!s + // increment the final result if s = 0
g( // add the result of a recursive call to g():
s > 0 ? // if s is positive:
s - ( // subtract from s the result of p():
p = d => k % --d ? // p() = recursive helper function looking
p(d) // for the highest divisor d of k,
: // starting with d = k - 1
d < 2 && // if d is less than 2 (i.e. k is prime):
a.push(k) && // append k to a[]
k // and return k (else: return false)
)(++k) // increment k and call p(k)
: // else:
s + a.shift() // remove the first entry from a[]
// and add it to s
) // end of recursive call
)(n) // initial call to g() with s = n
Physica, 41 bytes
->x:Count[Sum@Sublists[PrimeQ$$[…x]];x]
How it works
->x:Count[Sum@Sublists[PrimeQ$$[…x]];x] // Full program.
->x: // Define an anonymous function with parameter x.
[…x] // Range [0 ... x] (inclusive).
$$ // Filter-keep those that...
PrimeQ // Are prime.
Sublists[...] // Get all their sublists.
Sum@ // Then sum each sublist.
Count[...;x] // Count the number of times x occurs in the result.
Husk, 9 8 bytes
-1 byte thanks to Mr.Xcoder (use named argument ¹ instead of S)!
#¹ṁ∫ṫ↑İp
Explanation
#¹ṁ∫ṫ↑İp -- example input: 3
#¹ -- count the occurrences of 3 in
İp -- | primes: [2,3,5,7..]
↑ -- | take 3: [2,3,5]
ṫ -- | tails: [[2,3,5],[3,5],[5]]
ṁ -- | map and flatten
∫ -- | | cumulative sums
-- | : [2,5,10,3,8,5]
-- : 1
Jelly, 6 5 bytes
-1 thanks to dylnan
ÆRẆ§ċ
A monadic link
Try it online! Or see the test-suite (note the final test case would time out at 60s at TIO).
How?
ÆRẆ§ċ - Link: integer, n
ÆR - primes from 2 to n inclusive
Ẇ - all contiguous substrings
§ - sum each
ċ - count occurrences of n
Retina 0.8.2, 68 bytes
.+
$*_$&$*
_
$`__¶
A`^(__+)\1+$
m)&`^((_)|¶)+¶[_¶]*(?<-2>1)+$(?(2)1)
Try it online! Link includes faster test cases. Explanation:
m)
Run the whole script in multiline mode where ^ and $ match on every line.
.+
$*_$&$*
Convert to unary twice, first using _s, then using 1s.
_
$`__¶
Generate all the prefixes of the _s and add two to each, thus counting from \$2\$ to \$n + 1\$.
A`^(__+)\1+$
Delete all the composite numbers in the range.
&`^((_)|¶)+¶[_¶]*(?<-2>1)+$(?(2)1)
Match any number of _s and newlines (starting and ending at the end of a line) then compare the number of _s with the number of 1s. Count the number of sums of consecutive prime numbers which add up to \$n\$.