(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++;
]
]
]
Returnin the body of theWhile. Also you knowSubsetsis a thing, right? Like it's a function you can use. Generally a procedural implementation of a function will be less efficient. – b3m2a1 Sep 16 '17 at 02:05Returnat all. Just putmyPowerSetat the end of theModule. – b3m2a1 Sep 16 '17 at 02:06AppendTo. I don't know what your input is, but I think you should really read about the language before you go any further. MSE is not a code debugging service and you seem to need a good primer on the language. By the way, just do this:Subsets[{a, b, c}]. – b3m2a1 Sep 16 '17 at 02:11subsetsfunction from my answer is still 16 times slower than the built-in version. – b3m2a1 Sep 16 '17 at 06:26