(The Goal is to create a program that given an input set, yields an output showing the Power Set of the entered set without using the built-in mathematica function" "When I enter this code into Mathmetica and run it, it ONLY GOES THROUGH THE LOOP ONCE AND ENDS THERE. Not sure what I'm doing wrong.)
MyIterativeSubsets[inlist_] :=
Module[
{
listLength,
powerSetLength,
myPowerSet,
pointer,
counter,
currentElement,
newSet,
DebugFlag = True
},
If[! ListQ[inlist], Return["Please enter a list"],
listLength = Length[inlist];
myPowerSet = {{}};
pointer = 1;
If[DebugFlag == True,
Print["What was entered"];
Print[inlist];
Print["List Length"];
Print[listLength];
Print["Power Set is"];
Print[myPowerSet];
];
While[pointer <= listLength,
currentElement = inlist[[pointer]];
powerSetLength = Length[myPowerSet];
counter = 1;
newSet = {};
If[DebugFlag == True,
Print["Current Element is"];
Print[currentElement];
Print["powerSetLength is"];
Print[powerSetLength];
Print["Counter is"];
Print[counter];
];
While[counter <= powerSetLength,
newSet =
Append[newSet, Append[myPowerSet[[counter]], currentElement]];
myPowerSet = Union[myPowerSet, newSet];
counter++;
If[DebugFlag == True,
Print["New Set is"];
Print[newSet];
Print["My Power Set is"];
Print[myPowerSet];
Print["Counter is"];
Print[counter];
];
];
Print["This is the current Power Set"];
Return[myPowerSet]
pointer++;
]
]
]
Return
in the body of theWhile
. Also you knowSubsets
is a thing, right? Like it's a function you can use. Generally a procedural implementation of a function will be less efficient. $\endgroup$Return
at all. Just putmyPowerSet
at the end of theModule
. $\endgroup$subsets
function from my answer is still 16 times slower than the built-in version. $\endgroup$