Partition an integer x into n primes: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 45: | Line 45: | ||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="11l">F is_prime(a) |
||
R !(a < 2 | any((2 .. Int(a ^ 0.5)).map(x -> @a % x == 0))) |
R !(a < 2 | any((2 .. Int(a ^ 0.5)).map(x -> @a % x == 0))) |
||
Line 86: | Line 86: | ||
L(n, cnt) data |
L(n, cnt) data |
||
partition(n, cnt)</ |
partition(n, cnt)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 104: | Line 104: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{works with|C99}} |
{{works with|C99}} |
||
< |
<syntaxhighlight lang="c">#include <assert.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
Line 232: | Line 232: | ||
sieve_destroy(&s); |
sieve_destroy(&s); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 250: | Line 250: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
{{works with|C sharp|7}} |
{{works with|C sharp|7}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections; |
using System.Collections; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 327: | Line 327: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 344: | Line 344: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <functional> |
#include <functional> |
||
#include <iostream> |
#include <iostream> |
||
Line 482: | Line 482: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Partitioned 99809 with 1 prime 99809 |
<pre>Partitioned 99809 with 1 prime 99809 |
||
Line 497: | Line 497: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="d">import std.array : array; |
||
import std.range : take; |
import std.range : take; |
||
import std.stdio; |
import std.stdio; |
||
Line 613: | Line 613: | ||
partition(p[0], p[1]); |
partition(p[0], p[1]); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 629: | Line 629: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Partition an integer as the sum of n primes. Nigel Galloway: November 27th., 2017 |
// Partition an integer as the sum of n primes. Nigel Galloway: November 27th., 2017 |
||
let rcTask n ng = |
let rcTask n ng = |
||
Line 639: | Line 639: | ||
|Some n->printfn "%d is the sum of %A" ng n |
|Some n->printfn "%d is the sum of %A" ng n |
||
|_ ->printfn "No Solution" |
|_ ->printfn "No Solution" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 655: | Line 655: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: formatting fry grouping kernel math.combinatorics |
||
math.parser math.primes sequences ; |
math.parser math.primes sequences ; |
||
Line 668: | Line 668: | ||
{ 99809 1 18 2 19 3 20 4 2017 24 22699 1 22699 2 22699 3 22699 |
{ 99809 1 18 2 19 3 20 4 2017 24 22699 1 22699 2 22699 3 22699 |
||
4 40355 3 } 2 group |
4 40355 3 } 2 group |
||
[ first2 2dup partition print-partition ] each</ |
[ first2 2dup partition print-partition ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 686: | Line 686: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
... though uses a sieve to generate the relevant primes. |
... though uses a sieve to generate the relevant primes. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 797: | Line 797: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 814: | Line 814: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (delete, intercalate) |
||
import Data.Numbers.Primes (primes) |
import Data.Numbers.Primes (primes) |
||
import Data.Bool (bool) |
import Data.Bool (bool) |
||
Line 858: | Line 858: | ||
------------------------- GENERIC ------------------------- |
------------------------- GENERIC ------------------------- |
||
justifyLeft :: Int -> Char -> String -> String |
justifyLeft :: Int -> Char -> String -> String |
||
justifyLeft n c s = take n (s ++ replicate n c)</ |
justifyLeft n c s = take n (s ++ replicate n c)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>(99809,1) -> 99809 |
<pre>(99809,1) -> 99809 |
||
Line 872: | Line 872: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
load 'format/printf' |
load 'format/printf' |
||
Line 914: | Line 914: | ||
tests=: (99809 1) ; (18 2) ; (19 3) ; (20 4) ; (2017 24) ; (22699 1) ; (22699 2) ; (22699 3) ; (22699 4) |
tests=: (99809 1) ; (18 2) ; (19 3) ; (20 4) ; (2017 24) ; (22699 1) ; (22699 2) ; (22699 3) ; (22699 4) |
||
(0&{ partitioned_in 1&{) each tests |
(0&{ partitioned_in 1&{) each tests |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 933: | Line 933: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
import java.util.stream.IntStream; |
import java.util.stream.IntStream; |
||
Line 1,009: | Line 1,009: | ||
partition(40355, 3); |
partition(40355, 3); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Partitioned 99809 with 1 prime: 99809 |
<pre>Partitioned 99809 with 1 prime: 99809 |
||
Line 1,026: | Line 1,026: | ||
'''Prime-number functions''' |
'''Prime-number functions''' |
||
< |
<syntaxhighlight lang="jq"> |
||
# Is the input integer a prime? |
# Is the input integer a prime? |
||
def is_prime: |
def is_prime: |
||
Line 1,071: | Line 1,071: | ||
| primes | if . > $x then break $out else . end; |
| primes | if . > $x then break $out else . end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Helper function''' |
'''Helper function''' |
||
< |
<syntaxhighlight lang="jq"># Emit a stream consisting of arrays, a, of $n items from the input array, |
||
# preserving order, subject to (a|add) == $sum |
# preserving order, subject to (a|add) == $sum |
||
def take($n; $sum): |
def take($n; $sum): |
||
Line 1,086: | Line 1,086: | ||
end |
end |
||
end; |
end; |
||
[$n, ., $sum] | take;</ |
[$n, ., $sum] | take;</syntaxhighlight> |
||
'''Partitioning an integer into $n primes''' |
'''Partitioning an integer into $n primes''' |
||
< |
<syntaxhighlight lang="jq"># This function emits a possibly empty stream of arrays. |
||
# Assuming $primes is primes(.), each array corresponds to a |
# Assuming $primes is primes(.), each array corresponds to a |
||
# partition of the input into $n distinct primes. |
# partition of the input into $n distinct primes. |
||
Line 1,125: | Line 1,125: | ||
else "A partition of \($x) into \($n) parts: \(first($x | primepartition($n)) | pp )" |
else "A partition of \($x) into \($n) parts: \(first($x | primepartition($n)) | pp )" |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''The tasks''' |
'''The tasks''' |
||
< |
<syntaxhighlight lang="jq">task(18; 2), |
||
task(19; 3), |
task(19; 3), |
||
task(20; 4), |
task(20; 4), |
||
task(2017; 24), |
task(2017; 24), |
||
task(22699; [1,2,3,4]), |
task(22699; [1,2,3,4]), |
||
task(40355; 3)</ |
task(40355; 3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,147: | Line 1,147: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
< |
<syntaxhighlight lang="julia">using Primes, Combinatorics |
||
function primepartition(x::Int64, n::Int64) |
function primepartition(x::Int64, n::Int64) |
||
Line 1,167: | Line 1,167: | ||
println("Partition of ", x, " into ", n, " primes: ", |
println("Partition of ", x, " into ", n, " primes: ", |
||
isempty(ans) ? "impossible" : join(ans, " + ")) |
isempty(ans) ? "impossible" : join(ans, " + ")) |
||
end</ |
end</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 1,185: | Line 1,185: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
// compiled with flag -Xcoroutines=enable to suppress 'experimental' warning |
// compiled with flag -Xcoroutines=enable to suppress 'experimental' warning |
||
Line 1,268: | Line 1,268: | ||
) |
) |
||
for (p in a) partition(p.first, p.second) |
for (p in a) partition(p.first, p.second) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,287: | Line 1,287: | ||
Using the prime generator class "sieve" from task [[Extensible prime generator#Lingo]]. |
Using the prime generator class "sieve" from task [[Extensible prime generator#Lingo]]. |
||
< |
<syntaxhighlight lang="lingo">---------------------------------------- |
||
-- returns a sorted list of the <cnt> smallest unique primes that add up to <n>, |
-- returns a sorted list of the <cnt> smallest unique primes that add up to <n>, |
||
-- or FALSE if there is no such partition of primes for <n> |
-- or FALSE if there is no such partition of primes for <n> |
||
Line 1,341: | Line 1,341: | ||
delete char (str.length+1-delim.length) to str.length of str |
delete char (str.length+1-delim.length) to str.length of str |
||
return str |
return str |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lingo">-- main |
||
_global.sieve = script("sieve").new() |
_global.sieve = script("sieve").new() |
||
Line 1,355: | Line 1,355: | ||
showPrimePartition(22699, 3) |
showPrimePartition(22699, 3) |
||
showPrimePartition(22699, 4) |
showPrimePartition(22699, 4) |
||
showPrimePartition(40355, 3)</ |
showPrimePartition(40355, 3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,372: | Line 1,372: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">NextPrimeMemo[n_] := (NextPrimeMemo[n] = NextPrime[n]);(*This improves performance by 30% or so*) |
||
PrimeList[count_] := Prime/@Range[count];(*Just a helper to create an initial list of primes of the desired length*) |
PrimeList[count_] := Prime/@Range[count];(*Just a helper to create an initial list of primes of the desired length*) |
||
AppendPrime[list_] := Append[list,NextPrimeMemo[Last@list]];(*Another helper that makes creating the next candidate less verbose*) |
AppendPrime[list_] := Append[list,NextPrimeMemo[Last@list]];(*Another helper that makes creating the next candidate less verbose*) |
||
Line 1,412: | Line 1,412: | ||
TimedResults = ReleaseHold[Hold[AbsoluteTiming[FormatResult[PrimePartition @@ #, Last@#]]] & /@TestCases](*I thought it would be interesting to include the timings, which are in seconds*) |
TimedResults = ReleaseHold[Hold[AbsoluteTiming[FormatResult[PrimePartition @@ #, Last@#]]] & /@TestCases](*I thought it would be interesting to include the timings, which are in seconds*) |
||
TimedResults // TableForm</ |
TimedResults // TableForm</syntaxhighlight> |
||
Line 1,428: | Line 1,428: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import math, sugar |
||
const N = 100_000 |
const N = 100_000 |
||
Line 1,475: | Line 1,475: | ||
echo n, " cannot be partitionned into ", k, " prime", plural(k) |
echo n, " cannot be partitionned into ", k, " prime", plural(k) |
||
else: |
else: |
||
echo n, " = ", part.join(" + ")</ |
echo n, " = ", part.join(" + ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,490: | Line 1,490: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">partDistinctPrimes(x,n,mn=2)= |
||
{ |
{ |
||
if(n==1, return(if(isprime(x) && mn<=x, [x], 0))); |
if(n==1, return(if(isprime(x) && mn<=x, [x], 0))); |
||
Line 1,525: | Line 1,525: | ||
} |
} |
||
V=[[99809,1], [18,2], [19,3], [20,4], [2017,24], [22699,1], [22699,2], [22699,3], [22699,4], [40355,3]]; |
V=[[99809,1], [18,2], [19,3], [20,4], [2017,24], [22699,1], [22699,2], [22699,3], [22699,4], [40355,3]]; |
||
for(i=1,#V, call(displayNicely, V[i]))</ |
for(i=1,#V, call(displayNicely, V[i]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Partitioned 99809 with 1 prime: 99809 |
<pre>Partitioned 99809 with 1 prime: 99809 |
||
Line 1,541: | Line 1,541: | ||
It is tempting to use the partition iterator which takes a "isprime" flag, but this task calls for unique values. Hence the combination iterator over an array of primes makes more sense. |
It is tempting to use the partition iterator which takes a "isprime" flag, but this task calls for unique values. Hence the combination iterator over an array of primes makes more sense. |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use ntheory ":all"; |
||
sub prime_partition { |
sub prime_partition { |
||
Line 1,555: | Line 1,555: | ||
my $partar = prime_partition(@$test); |
my $partar = prime_partition(@$test); |
||
printf "Partition %5d into %2d prime piece%s %s\n", $test->[0], $test->[1], ($test->[1] == 1) ? ": " : "s:", defined($partar) ? join("+",@$partar) : "not possible"; |
printf "Partition %5d into %2d prime piece%s %s\n", $test->[0], $test->[1], ($test->[1] == 1) ? ": " : "s:", defined($partar) ? join("+",@$partar) : "not possible"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,572: | Line 1,572: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (join(fmt))</span> |
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (join(fmt))</span> |
||
Line 1,609: | Line 1,609: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Partition %d into %d primes: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Partition %d into %d primes: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,626: | Line 1,626: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
< |
<syntaxhighlight lang="prolog">prime_partition(N, 1, [N], Min):- |
||
is_prime(N), |
is_prime(N), |
||
N > Min, |
N > Min, |
||
Line 1,669: | Line 1,669: | ||
print_prime_partition(22699, 3), |
print_prime_partition(22699, 3), |
||
print_prime_partition(22699, 4), |
print_prime_partition(22699, 4), |
||
print_prime_partition(40355, 3).</ |
print_prime_partition(40355, 3).</syntaxhighlight> |
||
Module for finding prime numbers up to some limit: |
Module for finding prime numbers up to some limit: |
||
< |
<syntaxhighlight lang="prolog">:- module(prime_numbers, [find_prime_numbers/1, is_prime/1]). |
||
:- dynamic is_prime/1. |
:- dynamic is_prime/1. |
||
Line 1,713: | Line 1,713: | ||
cross_out(S, N, P):- |
cross_out(S, N, P):- |
||
Q is S + 2 * P, |
Q is S + 2 * P, |
||
cross_out(Q, N, P).</ |
cross_out(Q, N, P).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,730: | Line 1,730: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">from itertools import combinations as cmb |
||
Line 1,764: | Line 1,764: | ||
except StopIteration: |
except StopIteration: |
||
print(repr((n, cnt)) + " -> Not possible") |
print(repr((n, cnt)) + " -> Not possible") |
||
break</ |
break</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(99809, 1) -> 99809 |
<pre>(99809, 1) -> 99809 |
||
Line 1,778: | Line 1,778: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require math/number-theory) |
(require math/number-theory) |
||
Line 1,810: | Line 1,810: | ||
(report-partition 22699 3) |
(report-partition 22699 3) |
||
(report-partition 22699 4) |
(report-partition 22699 4) |
||
(report-partition 40355 3))</ |
(report-partition 40355 3))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,829: | Line 1,829: | ||
{{works with|Rakudo|2018.10}} |
{{works with|Rakudo|2018.10}} |
||
<lang |
<syntaxhighlight lang="raku" line>use Math::Primesieve; |
||
my $sieve = Math::Primesieve.new; |
my $sieve = Math::Primesieve.new; |
||
Line 1,846: | Line 1,846: | ||
say (sprintf "Partition %5d into %2d prime piece", $number, $parts), |
say (sprintf "Partition %5d into %2d prime piece", $number, $parts), |
||
$parts == 1 ?? ': ' !! 's: ', join '+', partition($number, $parts) || 'not possible' |
$parts == 1 ?? ': ' !! 's: ', join '+', partition($number, $parts) || 'not possible' |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Partition 18 into 2 prime pieces: 5+13 |
<pre>Partition 18 into 2 prime pieces: 5+13 |
||
Line 1,866: | Line 1,866: | ||
which means: partition all integers (inclusive) from '''X''' ──► '''Y''' with '''N''' ──► '''M''' primes. |
which means: partition all integers (inclusive) from '''X''' ──► '''Y''' with '''N''' ──► '''M''' primes. |
||
<br>The ''to'' number ('''Y''' or '''M''') can be omitted. |
<br>The ''to'' number ('''Y''' or '''M''') can be omitted. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program partitions integer(s) (greater than unity) into N primes. */ |
||
parse arg what /*obtain an optional list from the C.L.*/ |
parse arg what /*obtain an optional list from the C.L.*/ |
||
Line 1,917: | Line 1,917: | ||
end /*!*/ /* [↑] Is sum too low? Bump a prime.*/ |
end /*!*/ /* [↑] Is sum too low? Bump a prime.*/ |
||
say 'partitioned' center(g,9) "into" center(q, 5) list() |
say 'partitioned' center(g,9) "into" center(q, 5) list() |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= when using the input of: <tt> 99809 1 18 2 19 3 20 4 2017 24 22699 1-4 40355 </tt>}} |
{{out|output|text= when using the input of: <tt> 99809 1 18 2 19 3 20 4 2017 24 22699 1-4 40355 </tt>}} |
||
<pre> |
<pre> |
||
Line 1,933: | Line 1,933: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Partition an integer X into N primes |
# Project : Partition an integer X into N primes |
||
Line 2,005: | Line 2,005: | ||
next |
next |
||
return items |
return items |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,021: | Line 2,021: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require "prime" |
||
def prime_partition(x, n) |
def prime_partition(x, n) |
||
Line 2,035: | Line 2,035: | ||
puts "Partitioned #{prime} with #{num} primes: #{str}" |
puts "Partitioned #{prime} with #{num} primes: #{str}" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Partitioned 99809 with 1 primes: 99809 |
<pre>Partitioned 99809 with 1 primes: 99809 |
||
Line 2,051: | Line 2,051: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="rust">// main.rs |
||
mod bit_array; |
mod bit_array; |
||
mod prime_sieve; |
mod prime_sieve; |
||
Line 2,108: | Line 2,108: | ||
print_prime_partition(&s, 22699, 4); |
print_prime_partition(&s, 22699, 4); |
||
print_prime_partition(&s, 40355, 3); |
print_prime_partition(&s, 40355, 3); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="rust">// prime_sieve.rs |
||
use crate::bit_array; |
use crate::bit_array; |
||
Line 2,145: | Line 2,145: | ||
!self.composite.get(n / 2 - 1) |
!self.composite.get(n / 2 - 1) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="rust">// bit_array.rs |
||
pub struct BitArray { |
pub struct BitArray { |
||
array: Vec<u32>, |
array: Vec<u32>, |
||
Line 2,170: | Line 2,170: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,187: | Line 2,187: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object PartitionInteger { |
||
def sieve(nums: LazyList[Int]): LazyList[Int] = |
def sieve(nums: LazyList[Int]): LazyList[Int] = |
||
Line 2,235: | Line 2,235: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">func prime_partition(num, parts) { |
||
if (parts == 1) { |
if (parts == 1) { |
||
Line 2,262: | Line 2,262: | ||
say ("Partition %5d into %2d prime piece" % (num, parts), |
say ("Partition %5d into %2d prime piece" % (num, parts), |
||
parts == 1 ? ': ' : 's: ', prime_partition(num, parts).join('+') || 'not possible') |
parts == 1 ? ': ' : 's: ', prime_partition(num, parts).join('+') || 'not possible') |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Partition 18 into 2 prime pieces: 5+13 |
<pre>Partition 18 into 2 prime pieces: 5+13 |
||
Line 2,278: | Line 2,278: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|Rust}} |
{{trans|Rust}} |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
class BitArray { |
class BitArray { |
||
Line 2,381: | Line 2,381: | ||
printPrimePartition(sieve: sieve, number: 22699, count: 3) |
printPrimePartition(sieve: sieve, number: 22699, count: 3) |
||
printPrimePartition(sieve: sieve, number: 22699, count: 4) |
printPrimePartition(sieve: sieve, number: 22699, count: 4) |
||
printPrimePartition(sieve: sieve, number: 40355, count: 3)</ |
printPrimePartition(sieve: sieve, number: 40355, count: 3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,399: | Line 2,399: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
{{trans|Rexx}} |
{{trans|Rexx}} |
||
< |
<syntaxhighlight lang="vb">' Partition an integer X into N primes |
||
dim p(),a(32),b(32),v,g: redim p(64) |
dim p(),a(32),b(32),v,g: redim p(64) |
||
what="99809 1 18 2 19 3 20 4 2017 24 22699 1-4 40355 3" |
what="99809 1 18 2 19 3 20 4 2017 24 22699 1-4 40355 3" |
||
Line 2,462: | Line 2,462: | ||
wscript.echo "partition "&g&" into "&q&" "&list |
wscript.echo "partition "&g&" into "&q&" "&list |
||
end sub 'part |
end sub 'part |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,480: | Line 2,480: | ||
{{trans|Rexx}} |
{{trans|Rexx}} |
||
{{works with|Visual Basic .NET|2011}} |
{{works with|Visual Basic .NET|2011}} |
||
< |
<syntaxhighlight lang="vbnet">' Partition an integer X into N primes - 29/03/2017 |
||
Option Explicit On |
Option Explicit On |
||
Line 2,559: | Line 2,559: | ||
End Module 'PartitionIntoPrimes |
End Module 'PartitionIntoPrimes |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,579: | Line 2,579: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
The relevant primes are generated here by a sieve. |
The relevant primes are generated here by a sieve. |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int, Nums |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 2,632: | Line 2,632: | ||
[40355, 3] |
[40355, 3] |
||
] |
] |
||
for (p in a) partition.call(p[0], p[1])</ |
for (p in a) partition.call(p[0], p[1])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,650: | Line 2,650: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Using the prime generator from task [[Extensible prime generator#zkl]]. |
Using the prime generator from task [[Extensible prime generator#zkl]]. |
||
< |
<syntaxhighlight lang="zkl"> // Partition integer N into M unique primes |
||
fcn partition(N,M,idx=0,ps=List()){ |
fcn partition(N,M,idx=0,ps=List()){ |
||
var [const] sieve=Utils.Generator(Import("sieve").postponed_sieve); |
var [const] sieve=Utils.Generator(Import("sieve").postponed_sieve); |
||
Line 2,665: | Line 2,665: | ||
} |
} |
||
Void // no solution |
Void // no solution |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">foreach n,m in (T( T(18,2),T(19,3),T(99809,1),T(20,4),T(2017,24), |
||
T(22699,1),T(22699,2),T(22699,3),T(22699,4),T(40355,3), )){ |
T(22699,1),T(22699,2),T(22699,3),T(22699,4),T(40355,3), )){ |
||
ps:=partition(n,m); |
ps:=partition(n,m); |
||
if(ps) println("Partition %d with %d prime(s): %s".fmt(n,m,ps.concat("+"))); |
if(ps) println("Partition %d with %d prime(s): %s".fmt(n,m,ps.concat("+"))); |
||
else println("Can not partition %d with %d prime(s)".fmt(n,m)); |
else println("Can not partition %d with %d prime(s)".fmt(n,m)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |