Power set: Difference between revisions
m
→{{header|Fōrmulæ}}
m (RPL: add section) |
|||
(7 intermediate revisions by 3 users not shown) | |||
Line 1,642:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Power_set}}
'''Solution'''
No program needed. Power set is intrinsically supported in Fōrmulæ.
'''Case 1.''' Power set of the set {1, 2, 3, 4}
[[File:Fōrmulæ - Power set 01.png]]
[[File:Fōrmulæ - Power set 02.png]]
'''Case 2.''' The power set of the empty set is the set which contains itself.
[[File:Fōrmulæ - Power set 03.png]]
[[File:Fōrmulæ - Power set 04.png]]
'''Case 3.''' The power set of the set which contains only the empty set, has two subsets, the empty set and the set which contains the empty set
[[File:Fōrmulæ - Power set 05.png]]
[[File:Fōrmulæ - Power set 06.png]]
'''Case 4.''' Even when it is intrinsically supported, a program can be written:
[[File:Fōrmulæ - Power set 07.png]]
[[File:Fōrmulæ - Power set 08.png]]
[[File:Fōrmulæ - Power set 09.png]]
=={{header|GAP}}==
Line 3,618 ⟶ 3,644:
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program displays a power set; items may be anything (but can't have blanks).*/
text='one two three four'
n=words(text)
psi=0
Do k=0 To n
Do while
Parse Var cc elist '/' cc /* get i from comb's result string */
psl=''
psl=psl','word(text,e) /* add corresponding test word to set */
!.= 0▼
End
do p=1 for y; !.p= p▼
psl=substr(psl,2) /* get rid of leading comma
Say right(psi,2) '{'psl'}' /* show this element of the power set */
End
End
Exit
▲ end /*d*/
comb: Procedure
/***********************************************************************
* Returns the combinations of size digits out of things digits
* e.g. comb(4,2) -> ' 1 2/1 3/1 4/2 3/2 4/3 4/'
***********************************************************************/
Parse Arg things,size
▲ p= !.d
n=2**things-1
list=''
Do u=1 To n
co=combinations(u)
return 0</syntaxhighlight>▼
If co>'' Then
list=list '/' combinations(u)
End
Return substr(space(list),2) '/' /* remove leading / */
combinations: Procedure Expose things size
Parse Arg u
nc=0
bu=x2b(d2x(u))
bu1=space(translate(bu,' ',0),0)
If length(bu1)=size Then Do
ub=reverse(bu)
res=''
Do i=1 To things
If substr(ub,i,1)=1 Then res=res i
End
Return res
End
Else
{{out|output|text= when using the default input:}}
<pre>
Line 4,576 ⟶ 4,623:
We therefore use lists to represent sets which works fine here.
<syntaxhighlight lang="
var sets = [ [1, 2, 3, 4], [], [[]] ]
|