Partition an integer x into n primes: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: " + " output)
m (→‎{{header|REXX}}: split some DO-END statements, added/changed commands and whitespace, simplified a pluralizer.)
Line 1,581: Line 1,581:
do until what=='' /*possibly process a series of integers*/
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 what x n what; parse var x x '-' y /*get possible range and # partitions.*/
parse var n n '-' m /*get possible range and # partitions.*/
parse var n n '-' m /* " " " " " " */
if x=='' | x=="," then x=19 /*Not specified? Then use the default.*/
if x=='' | x=="," then x= 19 /*Not specified? Then use the default.*/
if y=='' | y=="," then y=x /* " " " " " " */
if y=='' | y=="," then y= x /* " " " " " " */
if n=='' | n=="," then n= 3 /* " " " " " " */
if n=='' | n=="," then n= 3 /* " " " " " " */
if m=='' | m=="," then m=n /* " " " " " " */
if m=='' | m=="," then m= n /* " " " " " " */
call genP y /*generate Y number of primes. */
call genP y /*generate Y number of primes. */
do g=x to y /*partition X ───► Y into partitions.*/
do g=x to y /*partition X ───► Y into partitions.*/
do q=n to m; call part; end /*q*/ /*partition G into Q primes. */
do q=n to m; call part /* " G into Q primes. */
end /*g*/
end /*q*/
end /*g*/
end /*until*/
end /*until*/
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: arg high; @.1=2; @.2=3; #=2 /*get highest prime, assign some vars. */
genP: arg high; @.1= 2; @.2= 3; #= 2 /*get highest prime, assign some vars. */
do j=@.#+2 by 2 until @.#>high /*only find odd primes from here on. */
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. */
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.*/
if j // @.k==0 then iterate j /*Is J divisible by P? Then ¬ prime.*/
end /*k*/ /* [↓] a prime (J) has been found. */
end /*k*/ /* [↓] a prime (J) has been found. */
#=#+1; @.#=j /*bump prime count; assign prime to @.*/
#= # + 1; @.#= j /*bump prime count; assign prime to @.*/
end /*j*/
end /*j*/
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
getP: procedure expose i. p. @.; parse arg z /*bump the prime in the partition list.*/
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
if i.z==0 then do; _= z - 1; i.z= i._; end
i.z=i.z+1; _=i.z; p.z=@._; return 0
i.z= i.z + 1; _= i.z; p.z= @._
return 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
list: _=p.1; if $==g then do j=2 to q; _=_ p.j; end; else _= '__(not_possible)'
list: _= p.1; if $==g then do j=2 to q; _= _ p.j
the= 'primes:'; if q==1 then the= 'prime: '; return the translate(_,"+ ",' _')
end /*j*/
else _= '__(not_possible)'
return 'prime' || word('s', 1 + (q==1)) translate(_, "+ ", ' _') /*plural?*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
part: i.=0; do j=1 for q; call getP j; end /*j*/
part: i.= 0; do j=1 for q; call getP j
do !=0 by 0; $=0 /*!: a DO variable for LEAVE & ITERATE*/
end /*j*/
do s=1 for q; $=$+p.s /* [↓] is sum of the primes too large?*/
do !=0 by 0; $= 0 /*!: a DO variable for LEAVE & ITERATE*/
if $>g then do; if s==1 then leave ! /*perform a quick exit?*/
do s=1 for q; $= $ + p.s /* [↓] is sum of the primes too large?*/
do k=s to q; i.k=0; end /*k*/
if $>g then do; if s==1 then leave ! /*perform a quick exit?*/
do r=s-1 to q; call getP r; end /*r*/
do k=s to q; i.k= 0; end /*k*/
iterate !
do r=s-1 to q; call getP r; end /*r*/
end
iterate !
end /*s*/
end
if $==g then leave /*is sum of the primes exactly right ? */
end /*s*/
if $ <g then do; call getP q; iterate; end
if $==g then leave /*is sum of the primes exactly right ? */
end /*!*/ /* [↑] Is sum too low? Bump a prime.*/
if $ <g then do; call getP q; iterate; end
say 'partitioned' center(g,9) "into" center(q, 5) list()
end /*!*/ /* [↑] Is sum too low? Bump a prime.*/
say 'partitioned' center(g,9) "into" center(q, 5) list()
return</lang>
return</lang>
{{out|output|text=&nbsp; when using the input of: &nbsp; <tt> 99809 1 &nbsp; 18 2 &nbsp; 19 3 &nbsp;20 4 &nbsp; 2017 24 &nbsp; 22699 1-4 &nbsp; 40355 </tt>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; <tt> 99809 1 &nbsp; 18 2 &nbsp; 19 3 &nbsp;20 4 &nbsp; 2017 24 &nbsp; 22699 1-4 &nbsp; 40355 </tt>}}