Anonymous user
Partition an integer x into n primes: Difference between revisions
m
→{{header|REXX}}: split some DO-END statements, added/changed commands and whitespace, simplified a pluralizer.
m (→{{header|Phix}}: " + " output) |
m (→{{header|REXX}}: split some DO-END statements, added/changed commands and whitespace, simplified a pluralizer.) |
||
Line 1,581:
do until what=='' /*possibly process a series of integers*/
parse var what x n what; parse var x x '-' y /*get possible range and # partitions.*/
parse var n n '-' m /*
if x=='' | x=="," then x=
if y=='' | y=="," then y=
if n=='' | n=="," then n=
if m=='' | m=="," then m=
call genP y /*generate Y number of primes. */
do g=x to y
do q=n to m; call part
end /*g*/
end /*until*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: arg high; @.1= 2;
do j=@.#+2 by 2 until @.#>high /*only find odd primes from here on. */
do k=2 while k*k<=j /*divide by some known low odd primes. */
if j // @.k==0 then iterate j /*Is J divisible by P? Then ¬ prime.*/
end /*k*/ /* [↓] a prime (J) has been found. */
#= # + 1;
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
getP: procedure expose i. p. @.; parse arg z /*bump the prime in the partition list.*/
if i.z==0 then do; _= z - 1; i.z= i._; end
i.z= i.z + 1; _= i.z; p.z= @._
return 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
list: _= p.1; if $==g then
else _= '__(not_possible)'
return 'prime' || word('s', 1 + (q==1)) translate(_, "+ ", ' _') /*plural?*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
part: i.= 0; do j=1 for q; call getP j
do !=0 by 0;
do s=1
if $>g then do; if
if $
say 'partitioned' center(g,9) "into" center(q, 5) list()
return</lang>
{{out|output|text= when using the input of: <tt> 99809 1 18 2 19 3 20 4 2017 24 22699 1-4 40355 </tt>}}
|