Partition an integer x into n primes: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add SETL) |
Not a robot (talk | contribs) (Add Refal) |
||
Line 2,064: | Line 2,064: | ||
Partition 40355 into 3 prime pieces: 3+139+40213</pre> |
Partition 40355 into 3 prime pieces: 3+139+40213</pre> |
||
=={{header|Refal}}== |
|||
<syntaxhighlight lang="refal">$ENTRY Go { |
|||
= <Each Test <Tests>>; |
|||
}; |
|||
Tests { |
|||
= (99809 1) (18 2) (19 3) (20 4) (2017 24) |
|||
(22699 1) (22699 2) (22699 3) (22699 4) (40355 3); |
|||
}; |
|||
Test { |
|||
(s.X s.N) = |
|||
<Prout 'Partitioned ' <Symb s.X> ' with ' <Symb s.N> ' primes: ' |
|||
<Format <PrimePartition s.X s.N>>>; |
|||
}; |
|||
Format { |
|||
F = 'not possible'; |
|||
T s.N = <Symb s.N>; |
|||
T s.N e.X = <Symb s.N> '+' <Format T e.X>; |
|||
}; |
|||
PrimePartition { |
|||
s.X s.N = <Partition s.X s.N <Primes s.X>>; |
|||
}; |
|||
Partition { |
|||
s.X 1 e.Nums, e.Nums: { |
|||
e.1 s.X e.2 = T s.X; |
|||
e.1 = F; |
|||
}; |
|||
s.X s.N = F; |
|||
s.X s.N s.Num e.Nums, <Compare s.X s.Num>: '-' = |
|||
<Partition s.X s.N e.Nums>; |
|||
s.X s.N s.Num e.Nums, |
|||
<Partition <- s.X s.Num> <- s.N 1> e.Nums>: { |
|||
T e.List = T s.Num e.List; |
|||
F = <Partition s.X s.N e.Nums>; |
|||
}; |
|||
}; |
|||
Primes { |
|||
s.N = <Sieve <Iota 2 s.N>>; |
|||
}; |
|||
Iota { |
|||
s.End s.End = s.End; |
|||
s.Start s.End = s.Start <Iota <+ 1 s.Start> s.End>; |
|||
}; |
|||
Cross { |
|||
s.Step e.List = <Cross (s.Step 1) s.Step e.List>; |
|||
(s.Step s.Skip) = ; |
|||
(s.Step 1) s.Item e.List = X <Cross (s.Step s.Step) e.List>; |
|||
(s.Step s.N) s.Item e.List = s.Item <Cross (s.Step <- s.N 1>) e.List>; |
|||
}; |
|||
Sieve { |
|||
= ; |
|||
X e.List = <Sieve e.List>; |
|||
s.N e.List = s.N <Sieve <Cross s.N e.List>>; |
|||
}; |
|||
Each { |
|||
s.F = ; |
|||
s.F t.X e.R = <Mu s.F t.X> <Each s.F e.R>; |
|||
};</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Partitioned 99809 with 1 primes: 99809 |
|||
Partitioned 18 with 2 primes: 5+13 |
|||
Partitioned 19 with 3 primes: 3+5+11 |
|||
Partitioned 20 with 4 primes: not possible |
|||
Partitioned 2017 with 24 primes: 2+3+5+7+11+13+17+19+23+29+31+37+41+43+47+53+59+61+67+71+73+79+97+1129 |
|||
Partitioned 22699 with 1 primes: 22699 |
|||
Partitioned 22699 with 2 primes: 2+22697 |
|||
Partitioned 22699 with 3 primes: 3+5+22691 |
|||
Partitioned 22699 with 4 primes: 2+3+43+22651 |
|||
Partitioned 40355 with 3 primes: 3+139+40213</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Usage note: entering ranges of '''X''' and '''N''' numbers (arguments) from the command line: |
Usage note: entering ranges of '''X''' and '''N''' numbers (arguments) from the command line: |