Counting and Enumerating the Number of Divisors of a Natural Number by James Pate Williams, Jr.

A simple number theoretic problem is to count and enumerate the number of divisors of a natural number which is the set { 1, 2, 3, … }. An Order(n) method is to find all numbers between 1 and n such that the number divides n. If you have the prime factorization of n then the number of divisors is the product of the prime factorization (exponents + 1). For example the divisors of 100 are:

1 2 4 5 10 20 25 50 100

The prime factorization of 100 = 2^2 * 5 ^ 2. So the number of divisors is (2 + 1) * (2 + 1) = 9.

Below is a C++ implementation of an algorithm to enumerate and count the number of divisors of a natural number and count the divisors by using the factorization found by trial division.

#include <algorithm>
#include <chrono>
#include <iostream>
#include <vector>
using namespace std;

const int B0 = 10000000;

bool sieve[B0 + 1];
vector<int> prime, divisors, expon, primes, primesSquares;

void Sieve()
{
	// Sieve of Eratosthenes
	// find all prime numbers
	// less than or equal B0

	int c = 3, i, inc;

	sieve[2] = true;

	for (i = 3; i <= B0; i++)
		if (i % 2 == 1)
			sieve[i] = true;

	do
	{
		i = c * c;
		inc = c + c;

		while (i <= B0)
		{
			sieve[i] = false;

			i += inc;
		}

		c += 2;

		while (!sieve[c])
			c++;

	} while (c * c <= B0);

	for (i = 2; i <= B0; i++)
	{
		if (sieve[i])
		{
			primes.push_back(i);
			primesSquares.push_back(i * i);
		}
	}
}

bool TrialDivision(int number)
{
	int bound = B0; // (int)sqrt(number);

	for (int i = 0; i < (int)primes.size(); i++)
	{
		int p = primes[i];

		if (p <= bound)
		{
			if (number % p == 0)
			{
				int e = 0;

				while (number % p == 0)
				{
					e++;
					number /= p;
				}

				prime.push_back(p);
				expon.push_back(e);
			}

			if (number == 1)
				return true;
		}
	}

	return false;
}

void GetDivisors(int n, int count)
{
	divisors.push_back(1);

	for (int i = 0; i < (int)prime.size(); i++)
	{
		int p = prime[i];

		for (int j = 1; j <= (int)expon[i]; j++)
		{
			int q = (int)pow(p, j);

			divisors.push_back(q);
		}
	}

	bool done = false;
	int limit;

	do
	{
		limit = (int)divisors.size();

		for (int i = 1; i < limit - 1; i++)
		{
			int di = divisors[i];

			for (int j = i + 1; !done && j < limit; j++)
			{
				int dj = divisors[j], product = di * dj;
				vector<int>::iterator it =
					find(divisors.begin(), divisors.end(), product);

				if (it == divisors.end())
				{
					if (divisors.size() < count &&
						product <= n && n % product == 0)
						divisors.push_back(product);

					else if (divisors.size() == count)
						done = true;
				}
			}
		}
	} while (!done);

	std::sort(divisors.begin(), divisors.end());
}

int main()
{
	int count = 0, number;

	std::cout << "Enter a number = ";
	cin >> number;
	std::cout << endl;

	auto start = chrono::high_resolution_clock::now();

	for (int i = 1; i <= number; i++)
	{
		if (number % i == 0)
		{
			cout << i << ' ';
			count++;
		}
	}

	auto finish = chrono::high_resolution_clock::now();

	std::cout << endl << endl;
	std::cout << "Divisor count = " << count << endl << endl;

	chrono::duration<double> elapsed = finish - start;

	std::cout << "Elapsed time = " << elapsed.count()
		<< " seconds" << endl << endl;

	start = chrono::high_resolution_clock::now();

	Sieve();

	if (!TrialDivision(number))
	{
		cout << "Trial division failed!" << endl;
		return 0;
	}

	count = 1;

	for (int i = 0; i < (int)expon.size(); i++)
		count *= expon[i] + 1;

	finish = chrono::high_resolution_clock::now();

	std::cout << "Divisor count = " << count << endl << endl;

	GetDivisors(number, count);

	for (int i = 0; i < (int)divisors.size(); i++)
		std::cout << divisors[i] << " ";

	std::cout << endl << endl;

	elapsed = finish - start;

	std::cout << "Elapsed time = " << elapsed.count()
		<< " seconds" << endl;
}

On Jigsaw Puzzle Solving and Computer Chess by James Pate Williams, Jr.

I have been playing a jigsaw puzzle app on my Microsoft desktop. The name of the app is “Jigsaw Puzzle HD”. I get one free puzzle per day. I set the number of pieces to 49 which is a 7 by 7 square. It takes me approximately 10 to 20 minutes to solve puzzle. The pieces are large on my Dell display but there is not room to spare using a maximized window and 49 pieces.

Here are some tips I have learned about jigsaw puzzle solving (an algorithm):

  • Roughly separate the pieces by color
  • Separate the four boundaries out from the sorted pieces
  • Construct the four boundaries of the puzzle
  • Use a sharper color sort to really untangle the middle pieces
  • Solve small areas of the middle of the puzzle
  • Iterate the preceding steps until the solution is found

I have a nice chess playing app on my desktop computer named “The Chess Lv. 100”. This chess game has 100 levels and Level 1’s Rating is 258 and Level 100’s Rating is 2300. I have a Level 12 Rating of 849 which is probably lower than my United States Chess Federation Rating back in the era 1968 to 1971. I play Level 8 to 12 computer opponents and sometimes venture as high as Level 25 which has a rating of 1094. Here is some information about the United States Chess Federation ratings:

US Chess Federation:

I do not have a simple algorithm for chess, but do not make blunders and try to look several moves into future before you move. Also a good working knowledge of openings, middle-games, and end-games helps.

Battleship Iowa (BB-61) Exterior Ballistics Revisited by James Pate Williams Jr

I modified my battleship Iowa application to use the following input data based on the textbook “Exterior ballistics, 1935” by Ernest Edward Herrmann of the United States Naval Academy:

We get the following data from the Ordnance Pamphlet 770 October 1941

https://eugeneleeslover.com/USN-GUNS-AND-RANGE-TABLES/OP-770-1.html

Modification of My Anagram Solver by James Pate Williams, Jr.

Sometimes in my group therapy, we play a game of taking an anagram and unscrambling the puzzle and determining all the words that can be created from the unscrambled anagram letters. Suppose we have the scrambled word “cimdteos then the following list is created using my new application.

1 demotics
2 domestic
3 ed
4 em
5 me
6 mo
7 om
8 to
9 ti
10 it
11 cs
12 med
13 mot
14 tom
15 tic
16 cit
17 sic
18 sci
19 demo
20 dome
21 mode
22 mote
23 tome
24 omit
25 tics
26 cits
27 stoic
28 sitcom
29 demotic
30 do
31 es
32 st
33 ts
34 mod
35 mes
36 ems
37 est
38 set
39 sit
40 tis
41 its
42 some
43 mets
44 stem
45 ties
46 site
47 domes
48 demos
49 modes
50 motes
51 tomes
52 smote
53 mites
54 emits
55 smite
56 times
57 items
58 cites
59 modest

New Anagram Solver Application by James Pate Williams, Jr.

An anagram is a scrambled or jumbled word. It is a permutation of the letters in a word. There are six permutations of a three letter word such as “the” and are “the”, “teh”, “hte”, “het”, “eth”, and “eht”. The number of permutations of a word consisting of n letters is n!, for example, 4! = 4 * 3 * 2 * 1 = 4 * 3! = 24, 5! = 5 * 4! = 120, etc. I wrote a program to brute force solve anagrams using an English language dictionary consisting of 152,512 words, a hash table, and a permutation generator. The hash table is generated from the English language dictionary by the formula first character integer ASCII value squared + the second character integer ASCII value . The base hash table entry for the word “an” is 65 * 65 + 78 = 4,303. There will be collisions or words with the same hash code. In this case there is a list of words for each hash code value. The application can solve 12 character anagrams in less than one hour on my computer where 12! = 479,001,600. Some anagrams can represent more than one word so a list of potential anagrams is created. Some example executions of the C# application are illustrated below.

Multiple Precision Signed Integer Package by James Pate Williams, Jr.

I very recently created a multiple precision signed integer package in C++ using the standard library and a base of 10. I then implemented two integer factoring algorithms trial division and Pollard’s Rho method. Trial division uses all the prime numbers <= 10000 and there are 1229 such primes. Due to the choice of language and the exceedingly small base the resulting application is awfully slow when compared to a similar C# application. The multiple precision signed integer package is largely based on translation of the Pascal source code found in “Prime Numbers and Computer Methods for Factorization” by Hans Riesel. The test number is 2 ^ 72 – 1 which is a Mersenne composite integer. The output large integers start with the number of base 10 digits in the number. For comparison I have included the output from my C# Big Integer factorization program as a screen shot.
Menu

0 Exit Application
1 Test of Package
2 Trial Division
3 Pollard Rho

2

n = 4722366482869645213695

Duration (min:sec.mil) = 00:08.784

n is composite, factors:

3 ^ 3 factor has 1 digits
5 factor has 1 digits
7 factor has 1 digits
13 factor has 2 digits
17 factor has 2 digits
19 factor has 2 digits
37 factor has 2 digits
73 factor has 2 digits
109 factor has 3 digits
241 factor has 3 digits
433 factor has 3 digits
Large prime 5 3 8 7 3 7


Menu

0 Exit Application
1 Test of Package
2 Trial Division
3 Pollard Rho

3

n = 4722366482869645213695

n is composite, factors:

 2 2 1
 1 3
 1 3
 4 3 5 1 5
 2 1 3
 4 1 2 4 1
 3 2 4 1
 3 1 0 9
 3 4 3 3
 5 3 8 7 3 7

 1 3
 1 3
 1 3
 1 5
 1 7
 2 1 3
 2 1 7
 2 1 9
 2 3 7
 2 7 3
 3 1 0 9
 3 2 4 1
 3 4 3 3
 5 3 8 7 3 7

Duration (min:sec.mil) = 00:02.197

My Chess Playing Computer Programs by James Pate Williams, Jr.

I started creating computer programs the Summer Semester of 2002 at Auburn University. I took a course named “Hand Held Software Development”. The course was taught by my research advisor Associate Professor Richard O. Chapman. I created a distributed chess playing client-server Internet system for human chess players. The program was built using the Palm Pilot’s Palm OS and its C language compiler. Later in circa 2006 I built a rule based and neural network chess program for a computer to play itself and for a human observer (voyeur). The language was Java. Then in 2015 I translated and enhanced my Java program by rewriting the code in C#. Below are pictures of one game.

Word to Words Game Solution by James Pate Williams, Jr.

The description of this alphabetic letter game is very facile. Given a word make as many other words as possible using the letters of the given initial word. But first before we enumerate the game solution, we need to refresh the reader’s memory of some elementary mathematics.
The binary number system also known as the base 2 number system is used by computers to perform arithmetic. The digits in the binary number system are 0 and 1. The numbers 0 to 15 in binary using only four binary digits are where ^ is the exponentiation operator (raising a number to a power) are:
0	0000
1	0001	2 ^ 0 = 1
2	0010	2 ^ 1 = 2
3	0011	2 ^ 1 + 2 ^ 0 = 2 + 1 = 3
4	0100	2 ^ 2 = 4
5	0101	2 ^ 2 + 2 ^ 0 = 4 + 1 = 5
6	0110	2 ^ 2 + 2 ^ 1 = 4 + 2 = 6
7	0111	2 ^ 2 + 2 ^ 1 + 2 ^ 0 = 4 + 2 + 1 = 7
8	1000	2 ^ 3 = 8
9	1001	2 ^ 3 + 2 ^ 0 = 8 + 1 = 9
10	1010	2 ^ 3 + 2 ^ 1 = 8 + 2 = 10
11	1011	2 ^ 3 + 2 ^ 1 + 2 ^ 0 = 8 + 2 + 1 = 11
12	1100	2 ^ 3 + 2 ^ 2 = 8 + 4 = 12
13	1101	2 ^ 3 + 2 ^ 2 + 2 ^ 0 = 8 + 4 + 1 = 13
14	1110	2 ^ 3 + 2 ^ 2 + 2 ^ 1 = 8 + 4 + 2 = 14
15	1111	2 ^ 3 + 2 ^ 2 + 2 ^ 1 + 2 ^ 0 = 8 + 4 + 2 + 1 = 15

An algorithm to convert a base 10 (decimal) number to base 2 (binary) number is given below:
Input n a base 10 number
Output b[0], b[1], b[2], … a finite binary string representing the decimal number

Integer i = 0

Do 
Integer nmod2 = n mod 2
Integer ndiv2 = n / 2

b[i] = nmod2 + ‘0’
i = i + 1

n = ndiv2

While n > 0

The b[i] will be in reverse order. For example, convert 12 from decimal to using four binary digits:
12 mod 2 = 0
12 div 2 = 6

b[0] = ‘0’

i = 1

n = 6

6 mod 2 = 0
6 div 2 = 3

b[1] = ‘0’

i = 2

n = 3

3 mod 2 = 1
3 div 2 = 1

i = 3

b[2] = ‘1’

n = 1

1 mod 2 = 1
1 div 2 = 0

b[3] = ‘1’

n = 0

So, the reversed binary string of digits is “0011”. And after reversing the string we have 12 is represented by the binary digits “1100”.

Next, we need to define a power set and its binary representation. The index power set of 4 objects which has 2 ^ 4 = 16 entries is specified in the following table:
0	0000
1	0001
2	0010
3	0011
4	0100
5	0101
6	0110
7	0111
8	1000
9	1001
10	1010
11	1011
12	1100
13	1101
14	1110
15	1111
A permutation of the set of three indices is given by the following list:
012, 021, 102, 120, 201, 210

A permutation of n objects is a list of n! = n * (n – 1) * … * 2 * 1. A permutation of 4 objects has a list of 24 – 4-digit indices list since 4! = 4 * 3 * 2 * 1 = 24 has the table:
0123, 0132, 0213, 0231, 0312, 0321,
1023, 1032, 1203, 1230, 1320, 1302,
2013, 2031, 2103, 2130, 2301, 2310,
3012, 3021, 3102, 3120, 3201, 3210

Suppose our word is “lost” then we first find the power set:

1	0001	t
2	0010	s
3	0011	st	ts
4	0100	o
5	0101	ot	to
6	0110	os	so
7	0111	ost	ots	sot	sto	tos	tso
8	1000	l
9	1001	lt	tl
10	1010	ls	sl
11	1011	lst	lts	slt	stl	tsl	tls
12	1100	lo	ol
13	1101	lot	lto	olt	otl	tlo	tol	
14	1110	los	lso	slo	sol	osl	ols
15	1111	lost	lots	slot	etc.

Using a dictionary of 152,512 English words my program finds 16 hits for the letters of “lost”:

Dictionary Length: 152512

Word: lost

  0     l
  1     lo
  2     lost
  3     lot
  4     lots
  5     ls
  6     o
  7     s
  8     slot
  9     so
 10     sol
 11     sot
 12     st
 13     t
 14     to
 15     ts

Total letters and/or words 16

Next, we use “tear” as our word:

Dictionary Length: 152512

Word: tear

  0     a
  1     are
  2     art
  3     at
  4     ate
  5     e
  6     ea
  7     ear
  8     eat
  9     era
 10     et
 11     eta
 12     r
 13     rat
 14     rate
 15     re
 16     rt
 17     rte
 18     t
 19     tar
 20     tare
 21     tea
 22     tear
 23     tr

Total letters and/or words 24

Finally, we use the word “company”:

Dictionary Length: 152512

Word: company

  0     a
  1     ac
  2     am
  3     amp
  4     an
  5     any
  6     c
  7     ca
  8     cam
  9     camp
 10     campy
 11     can
 12     canopy
 13     cap
 14     capo
 15     capon
 16     cay
 17     cm
 18     co
 19     com
 20     coma
 21     comp
 22     company
 23     con
 24     cony
 25     cop
 26     copay
 27     copy
 28     coy
 29     cyan
 30     m
 31     ma
 32     mac
 33     man
 34     many
 35     map
 36     may
 37     mayo
 38     mo
 39     moan
 40     mop
 41     mp
 42     my
 43     myna
 44     n
 45     nap
 46     nay
 47     nm
 48     no
 49     o
 50     om
 51     on
 52     op
 53     p
 54     pa
 55     pan
 56     pay
 57     pm
 58     pony
 59     y
 60     ya
 61     yam
 62     yap
 63     yo
 64     yon

Total letters and/or words 65

The C++ program's source code is given below:

// WordToWords.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> dictWords;

bool ReadDictionaryFile()
{
    fstream newfile;
 
    newfile.open("C:\\Users\\james\\source\\repos\\WordToWords\\Dictionary.txt", ios::in);

    if (newfile.is_open()) {
        int index = 0, length = 128;
        char cstr[128];

        while (newfile.getline(cstr, length)) {
            string str;

            str.clear();

            for (int i = 0; i < (int)strlen(cstr); i++)
                str.push_back(cstr[i]);

            dictWords.push_back(str);
        }

        newfile.close();

        sort(dictWords.begin(), dictWords.end());
        return true;
    }

    else
        return false;
}

string ConvertBase2(char cstr[], int n, int len)
{
    int count = 0;
    string str, rev;

    do
    {
        int nMod2 = n % 2;
        int nDiv2 = n / 2;

        str.push_back(nMod2 + '0');
        n = nDiv2;
    } while (n > 0);

    n = str.size();

    for (int i = n; i < len; i++)
        str.push_back('0');

    n = str.size();

    for (int i = n - 1; i >= 0; i--)
        if (str[i] == '1')
            rev.push_back(cstr[i]);

    return rev;
}

vector<string> PowerSet(char cstr[], int len)
{
    vector<int> index;
    vector<string> match;

    for (long ps = 0; ps <= pow(2, len); ps++)
    {
        string str = ConvertBase2(cstr, ps, len);
        int psf = 1;

        for (int i = 2; i <= len; i++)
            psf *= i;

        for (int i = 0; i < psf; i++)
        {
            if (binary_search(dictWords.begin(), dictWords.end(), str))
            {
                if (!binary_search(match.begin(), match.end(), str))
                {
                    match.push_back(str);
                    sort(match.begin(), match.end());
                }
            }

            next_permutation(str.begin(), str.end());
        }

        sort(match.begin(), match.end());
    }

    return match;
}

int main()
{
    bool done = false;
    char cstr[128];
    int len;
    string str;
    vector<int> index;
    vector<string> match;

    if (!ReadDictionaryFile())
        return -1;

    cout << "Dictionary Length: " << dictWords.size() << endl << endl;

    cout << "Word: ";
    cin >> cstr;
    cout << endl;

    len = strlen(cstr);

    if (len != 0)
    {
        vector<string> match = PowerSet(cstr, len);

        for (int i = 0; i < match.size(); i++)
        {
            cout << setprecision(3) << setw(3) << i << "\t";
            cout << match[i] << endl;
        }

        cout << endl;
        cout << "Total letters and/or words " << match.size() << endl;
        cout << endl;
    }
}

Schwarzschild Orbit by James Pate Williams, Jr.

The Rosette motion of the perihelion precession of a massive object orbiting a black hole (Schwarzschild solution Einstein’s general relativity field equations is illustrated by the graphs below for varying values of eccentricity of the ellipsoidal orbit 0.00, 0.01, 0.02, 0.03, 0.04, and 0.05. The angular momentum constant is B = 1, and the mass is M = 10.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SchwarzschildOrbit
{
    public partial class GraphForm : Form
    {
        private double B, B2, B4, M, M2, M3, epsilon;

        public GraphForm(double B, double M, double epsilon)
        {
            InitializeComponent();
            this.B = B;
            this.M = M;
            this.epsilon = epsilon;
            B2 = B * B;
            B4 = B2 * B2;
            M2 = M * M;
            M3 = M * M2;
            panel1.Paint += Panel1_Paint;
        }

        private double u0(double phi)
        {
            return M * (1.0 + epsilon * Math.Cos(phi)) / B2;
        }

        private double u1(double phi)
        {
            return u0(phi) + 3.0 * M3 * (1.0 + epsilon * phi * Math.Sin(phi)
                + epsilon * epsilon * (0.5 - Math.Cos(2.0 * phi) / 6.0)) / B4;
        }

        private double X(double r, double phi)
        {
            return r * Math.Cos(phi);
        }

        private double Y(double r, double phi)
        {
            return r * Math.Sin(phi);
        }

        private void Maximums(out double maxR, out double maxPhi,
            out double XMax, out double XMin, out double YMax, out double YMin)
        {
            double phi = 0.0, r = 0.0, XC = 0.0, YC = 0.0;

            maxPhi = 0.0;
            maxR = double.MinValue;
            XMax = double.MinValue;
            YMax = double.MinValue;
            XMin = double.MaxValue;
            YMin = double.MaxValue;

            while (phi <= 8.0 * Math.PI)
            {
                r = 1.0 / u1(phi);

                if (r > maxR)
                {
                    maxR = r;
                    maxPhi = phi;
                }

                XC = X(r, phi);

                if (XC > XMax)
                    XMax = XC;

                YC = Y(r, phi);

                if (YC > YMax)
                    YMax = YC;

                if (XC < XMin)
                    XMin = XC;

                if (YC < YMin)
                    YMin = YC;

                phi += 0.001;
            }
        }

        private void Minimums(out double minR, out double minPhi,
            out double XMax, out double XMin, out double YMax, out double YMin)
        { 
            double phi = 0.0, r = 0.0, XC = 0.0, YC = 0.0;

            minPhi = 0.0;
            minR = double.MaxValue;
            XMax = double.MinValue;
            YMax = double.MinValue;
            XMin = double.MaxValue;
            YMin = double.MaxValue;

            while (phi <= 8.0 * Math.PI)
            {
                r = 1.0 / u1(phi);

                if (r < minR)
                {
                    minR = r;
                    minPhi = phi;
                }

                XC = X(r, phi);

                if (XC > XMax)
                    XMax = XC;

                YC = Y(r, phi);

                if (YC > YMax)
                    YMax = YC;

                if (XC < XMin)
                    XMin = XC;

                if (YC < YMin)
                    YMin = YC;

                phi += 0.001;
            }
        }

        private void Panel1_Paint(object sender, PaintEventArgs e)
        {
            panel1.Size = ClientSize;
            int width = ClientSize.Width;
            int height = ClientSize.Height;
            int deltaX = width / 6;
            int deltaY = height / 6;
            int minX = deltaX;
            int maxX = 5 * deltaX;
            int minY = deltaY;
            int maxY = 5 * deltaY;
            double maxPhi, minPhi, maxR, minR;
            double XMax, XMin, YMax, YMin;
            double UMax, UMin, VMax, VMin;

            Maximums(out maxR, out maxPhi, out XMax, out XMin, out YMax, out YMin);
            Minimums(out minR, out minPhi, out UMax, out UMin, out VMax, out VMin);
            
            double slopeX = (maxX - minX) / (XMax - XMin);
            double slopeY = (minY - maxY) / (YMax - YMin);
            double interX = minX - slopeX * XMin;
            double interY = maxY - slopeY * YMin;
            double chi = 0.0, eta = 0.0, phi = 0.0, r = 0.0, x, y;
            Graphics g = e.Graphics;
            Pen bp = new Pen(Color.Black);
            SolidBrush rb = new SolidBrush(Color.Red);

            g.Clip = new Region(new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1));

            for (int i = 0; i < 5; i++)
                g.DrawLine(bp, (i + 1) * deltaX, minY, (i + 1) * deltaX, maxY);

            for (int i = 0; i < 5; i++)
                g.DrawLine(bp, minX, (i + 1) * deltaY, maxX, (i + 1) * deltaY);

            while (phi <= 8.0 * Math.PI)
            {
                r = 1.0 / u1(phi);
                x = X(r, phi);
                y = Y(r, phi);
                chi = slopeX * x + interX;
                eta = slopeY * y + interY;
                g.FillEllipse(rb, (float)chi, (float)eta, 1, 1);
                phi += 0.001;
            }
        }
    }
}

Word to Word Game by James Pate Williams, Jr.

I implemented a computerized version of a game similar to the board game Boggle. Given a jumble of letters find as many words as possible. Suppose we have the jumbled letters “sldfie” then my C++ program outputs:

Now suppose the scrambled letters are “nifrsmo”:

The program uses permutations and power sets. All permutations of the set of three numbers 1, 2, and 3 are:

123 132 213 231 312 321

The total number of permutations of n objects is n! where n! = n * (n – 1) * … * 2 * 1. So we have 3! = 3 * 2 * 1 = 6 and 4! = 4 * 3 * 2 * 1 = 24.


A power set of three objects can generated by using the binary representation of all binary numbers 0 to 2 * 2 * 2 – 1 = 8 – 1 = 7.

0 = 000, 1 = 001, 2 = 010, 3 = 011, 4 = 100, 5 = 101, 6 = 110, and 7 = 111

My main C++ function in the implementation is as follows:

vector<string> PowerSet(char cstr[], int len)
{
    vector<int> index;
    vector<string> match;

    for (long ps = 0; ps <= pow(2, len); ps++)
    {
        string str = ConvertBase2(cstr, ps, len);
        int psf = 1;

        for (int i = 2; i <= len; i++)
            psf *= i;

        for (int i = 0; i < psf; i++)
        {
            if (binary_search(dictWords.begin(), dictWords.end(), str))
            {
                if (!binary_search(match.begin(), match.end(), str))
                {
                    match.push_back(str);
                    sort(match.begin(), match.end());
                }
            }

            next_permutation(str.begin(), str.end());
        }
    }

    return match;
}