Powering Algorithms from Heri Cohen’s Textbook Implemented by James Pate Williams, Jr.

// Algorithms from "A Course in Computational
// Algebraic Number Theory" by Henri Cohen
// Implemented by James Pate Williams, Jr.
// Copyright (c) 2023 All Rights Reserved

using System.Collections.Generic;
using System.Numerics;

namespace PoweringAlgorithms
{
    public interface IAlgorithms
    {
        // Extended Euclidean Algorithm
        BigInteger Algorithm_1_2_1(
            BigInteger g, BigInteger n);
        BigInteger Algorithm_1_2_2(
            int e, BigInteger g, BigInteger n);
        public BigInteger Algorithm_1_2_3(
            int e, BigInteger g, BigInteger n);
        void Algorithm_1_3_6(
            BigInteger a, BigInteger b,
            out BigInteger u, out BigInteger v,
            out BigInteger d);
        List<int> GetBits(BigInteger N);
        BigInteger Inverse(
            BigInteger a, BigInteger n);
    }
}
using System.Collections.Generic;
using System.Numerics;

namespace PoweringAlgorithms
{
    public class Algorithms : IAlgorithms
    { 
        public BigInteger Algorithm_1_2_1(
            BigInteger g, BigInteger n)
        {
            BigInteger y = 1;

            if (n == 0)
                return y;

            BigInteger N, z;

            if (n < 0)
            {
                N = -n;
                z = Inverse(g, n);
            }

            else
            {
                N = n;
                z = g;
            }

            while (N > 0)
            {
                if (N % 2 == 1)
                    y *= z;

                N /= 2;
                z *= z;
            }

            return y;
        }

        public BigInteger Algorithm_1_2_2(
            int e, BigInteger g, BigInteger n)
        {
            BigInteger E, N, y, z;

            if (n == 0)
                return BigInteger.One;

            if (n < 0)
            {
                N = -n;
                z = Inverse(g, n);
            }

            else
            {
                N = n;
                z = g;
            }
            
            y = z;
            E = BigInteger.Pow(2, e);
            N -= E;

            while (E > 1)
            {
                E /= 2;
                y *= y;

                if (N >= E)
                {
                    N -= E;
                    y *= z;
                }
            }

            return y;
        }

        public BigInteger Algorithm_1_2_3(
            int e, BigInteger g, BigInteger n)
        {
            BigInteger y = 1;

            if (n == 0)
                return y;

            BigInteger N, z;

            if (n < 0)
            {
                N = -n;
                z = Inverse(g, n);
            }

            else
            {
                N = n;
                z = g;
            }

            y = z;

            int f = e;
            List<int> bits = GetBits(N);

            while (f > 0)
            {
                f--;
                y *= y;
                if (bits[f] == 1)
                    y *= z;
            }

            return y;
        }

        public void Algorithm_1_3_6(
                    BigInteger a, BigInteger b,
                    out BigInteger u, out BigInteger v,
                    out BigInteger d)
        {
            u = 1;
            d = a;

            if (b == 0)
            {
                v = 0;
                return;
            }

            BigInteger q, t1, t3, v1 = 0, v3 = b;

            while (v3 > 0)
            {
                q = d / v3;
                t3 = d % v3;
                t1 = u - q * v1;
                u = v1;
                d = v3;
                v1 = t1;
                v3 = t3;
            }

            v = (d - a * u) / b;
        }

        public List<int> GetBits(BigInteger N)
        {
            int b;
            List<int> temp = new();

            while (N > 0)
            {
                b = (int)(N % 2);
                N /= 2;
                temp.Add(b);
            }

            List<int> bits = new();

            for (int i = temp.Count; i >= 0; i--)
                bits.Add(temp[i]);

            return bits;
        }

        public BigInteger Inverse(BigInteger a, BigInteger n)
        {
            BigInteger d, u, v;

            Algorithm_1_3_6(
                a, n, out u, out v, out d);
            
            return u;
        }
    }
}
using System;
using System.Numerics;
using System.Windows.Forms;

namespace PoweringAlgorithms
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private static BigInteger Horner(string text)
        {
            BigInteger a = text[0] - '0';

            for (int i = 1; i < text.Length; i++)
                a = 10 * a + text[i] - '0';

            return a;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int expon = 0;
            Algorithms algos = new Algorithms();
            BigInteger g = Horner(textBox1.Text);
            BigInteger n = Horner(textBox2.Text);
            BigInteger a = algos.Algorithm_1_2_1(g, n);
            BigInteger m = BigInteger.Abs(n);

            while (m > 0)
            {
                expon++;
                m /= 2;
            }
            
            BigInteger b = algos.Algorithm_1_2_2(expon - 1, g, n);
            BigInteger c = algos.Algorithm_1_2_2(expon - 1, g, n);

            textBox3.Text = a.ToString();
            textBox4.Text = b.ToString();
            textBox5.Text = c.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

Infix Notation to Postfix Notation and Postfix Evaluator Implemented by James Pate Williams, Jr.

I translated a Pascal program that is found in “Applied Data Structures Using Pascal” by Guy J. Hale and Richard J. Easton. The original Pascal program only used single digits and four arithmetic operators: ‘*’, ‘/’, ‘+’, and ‘-‘. I extended the code to multiple digit positive integers and added an exponentiation operator ‘^’. The priority of the operators is ‘^’, ‘*’, and ‘/’ highest value and ‘+’ and ‘-‘ lowest priority. I could easily add a modulus operator ‘%’ and Boolean bit operators. Extension to negative integers should be a facile operation. Below is the output from my C++ application.

3 + 7 * 8 - 5
3 7 8 * + 5 - 
Positive integer value = 54
3 * 7 - 4 / 2
3 7 * 4 2 / - 
Positive integer value = 19
(3 + 7) * 8 - 5
3 7 + 8 * 5 - 
Positive integer value = 75
(3 + 4) * 8 - (7 * 3 - 4)
3 4 + 8 * 7 3 * 4 - - 
Positive integer value = 39
(100 + 50) * 20 - 100 / 2
100 50 + 20 * 100 2 / - 
Positive integer value = 2950
2 ^ 16 - 5 * 100
2 16 ^ 5 100 * - 
Positive integer value = 65036

#pragma once
#include <list>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const char Exp = '^';
const char Mul = '*';
const char Div = '/';
const char Add = '+';
const char Sub = '-';
const char LtParen = '(';
const char RtParen = ')';

class InfixToPostFix
{
public:
	stack<char> numberStack;
	
	int Convert(
		string infixStr, string& postfixStr);
	int EvaluatePostFix(string postfixStr);
	int Priority(char opcode);
};
#include "pch.h"
#include "InfixToPostFix.h"
#include <vector>
using namespace std;

int InfixToPostFix::Convert(
	string infixStr, string& postfixStr)
{
	char ch = 0;
	int number = 0, opcode = 0, opcode1 = 0, parenLevel = 0;
	string numberStr;

	for (size_t i = 0; i < infixStr.size();)
	{
		//while (i < infixStr.size() && infixStr[i] == ' ')
			//i++;

		while (i < infixStr.size() && infixStr[i] >= '0' &&
			infixStr[i] <= '9')
			numberStr.push_back(infixStr[i++]);

		if (numberStr.size() != 0)
		{
			for (size_t j = 0; j < numberStr.size(); j++)
				postfixStr.push_back(numberStr[j]);

			postfixStr.push_back(' ');
			numberStr.clear();
		}

		//while (i < infixStr.size() && infixStr[i] == ' ')
			//i++;

		if (infixStr[i] == '(')
		{
			numberStack.push(infixStr[i]);
			parenLevel++;
		}

		//while (i < infixStr.size() && infixStr[i] == ' ')
			//i++;

		if (infixStr[i] == ')')
		{
			ch = numberStack.top();
			numberStack.pop();

			parenLevel--;

			//while (i < infixStr.size() && infixStr[i] == ' ')
				//i++;

			while (ch != '(')
			{
				postfixStr.push_back(ch);
				postfixStr.push_back(' ');

				parenLevel++;

				ch = numberStack.top();
				numberStack.pop();
			}
		}

		//while (i < infixStr.size() && infixStr[i] == ' ')
			//i++;

		if (infixStr[i] == '^' || infixStr[i] == '*' ||
			infixStr[i] == '/' || infixStr[i] == '+' ||
			infixStr[i] == '-')
		{
			if (numberStack.empty())
				numberStack.push(infixStr[i]);
			else
			{
				ch = numberStack.top();
				numberStack.pop();

				//while (i < infixStr.size() && infixStr[i] == ' ')
					//i++;

				while (Priority(ch) >= Priority(infixStr[i]) &&
					!numberStack.empty() && ch != '(')
				{
					postfixStr.push_back(ch);
					postfixStr.push_back(' ');

					ch = numberStack.top();
					numberStack.pop();
				}

				//while (i < infixStr.size() && infixStr[i] == ' ')
					//i++;

				if (ch != '(')
				{
					if (Priority(infixStr[i]) <= Priority(ch))
					{
						postfixStr.push_back(ch);
						postfixStr.push_back(' ');

						numberStack.push(infixStr[i]);
					}

					else
					{
						numberStack.push(ch);
						numberStack.push(infixStr[i]);
					}
				}
				else
				{
					numberStack.push(ch);
					numberStack.push(infixStr[i]);
				}
			}
		}

		i++;
	}

	while (!numberStack.empty())
	{
		ch = numberStack.top();
		numberStack.pop();

		postfixStr.push_back(ch);
		postfixStr.push_back(' ');
	}

	return 0;
}

int InfixToPostFix::EvaluatePostFix(string postfixStr)
{
	char opcode = 0;
	int charValue = 0, result = 0, value1 = 0, value2 = 0;
	stack<int> intStack;

	for (size_t i = 0; i < postfixStr.size();)
	{
		string numberStr;

		if (postfixStr[i] != ' ')
		{
			while (postfixStr[i] >= '0' && postfixStr[i] <= '9')
				numberStr.push_back(postfixStr[i++]);

			if (!numberStr.empty())
				intStack.push(atoi(numberStr.c_str()));

			else
			{
				value2 = intStack.top();
				intStack.pop();
				value1 = intStack.top();
				intStack.pop();

				opcode = postfixStr[i++];

				if (opcode == '^')
					result = (int)pow(value1, value2);
				else if (opcode == '*')
					result = value1 * value2;
				else if (opcode == '/')
					result = value1 / value2;
				else if (opcode == '+')
					result = value1 + value2;
				else if (opcode == '-')
					result = value1 - value2;

				intStack.push(result);
			}
		}

		else
			i++;
	}

	result = intStack.top();
	intStack.pop();

	return result;
}

int InfixToPostFix::Priority(char opcode)
{
	int result = 0;

	switch (opcode)
	{
	case Exp:
		result = 2;
		break;
	case Mul:
		result = 2;
		break;
	case Div:
		result = 2;
		break;
	case Add:
		result = 1;
		break;
	case Sub:
		result = 1;
		break;
	case LtParen:
		result = 0;
	}

	return result;
}
#include "pch.h"
#include "InfixToPostFix.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    fstream inps, outs;
    char line[256];
    
    inps.open("TestFile.txt", fstream::in);
    outs.open("ResuFile.txt", fstream::out | fstream::ate);

    while (!inps.eof())
    {
        string postfixStr;

        inps.getline(line, 256);

        if (strlen(line) == 0)
            break;

        string infixStr(line, strlen(line));
        InfixToPostFix translate;

        int con = translate.Convert(
            infixStr, postfixStr);

        if (con != 0)
        {
            cout << "Conversion error!" << endl;
            cout << "Error code = " << con << endl;
        }

        else
        {
            char newline[] = { '\n' };

            outs.write(infixStr.c_str(), infixStr.size());
            outs.write(newline, 1);
            outs.write(postfixStr.c_str(), postfixStr.size());
            outs.write(newline, 1);

            int val = translate.EvaluatePostFix(postfixStr);

            if (val < 0)
            {
                cout << "Evaluation error!" << endl;
                cout << "Error code = " << val << endl;
            }

            else
            {
                char buffer[256] = { '\0' };
                string str = "Positive integer value = ";

                _itoa_s(val, buffer, 10);
                
                outs.write(str.c_str(), strlen(str.c_str()));
                outs.write(buffer, strlen(buffer));
                outs.write(newline, 1);
            }
        }
    }

    inps.close();
    outs.close();

    return 0;
}

Arithmetic Expression Evaluator in C++ Implemented by James Pate Williams, Jr.

I translated to C++ and enhanced a Pascal arithmetic expression evaluator program found in “Applied Data Structures Using Pascal” by Guy J. Hale and Richard E. Easton. The original code used single digit numbers. As an exercise I enhanced the application to utilize multiple digit numbers. Below is my test file and output from my program. I used the C++ standard library stack data structure.

20 + 3 * 4 + 50 * 4 * 2 + 6 * 2 - 8 / 2 + 2 ^ 5
#pragma once
#include <fstream>
#include <stack>
using namespace std;

class Expression
{
public:
	char ch, sign, termOp;
	int number;

	stack<int> stk;

	void GetChar(fstream& inps);
	void GetExpression(fstream& inps);
	void GetFactor(fstream& inps);
	void GetTerm(fstream& inps);
};
#include "pch.h"
#include "Expression.h"
#include <math.h>
#include <fstream>
#include <stack>
#include <string>
using namespace std;

void Expression::GetChar(
	fstream& inps)
{
	while (!inps.eof())
	{
		ch = (char)inps.get();

		if (inps.eof())
			exit(1);

		if (ch >= '0' && ch <= '9')
			return;

		if (ch == '^' || ch == '*' || ch == '/')
			return;

		if (ch == '+' || ch == '-')
			return;

		if (ch == ' ' || ch == '\t' || ch == '\n')
			continue;

		if (ch == ';')
			return;
	}
}

void Expression::GetExpression(fstream& inps)
{
	int num, num1, num2;

	if (ch == '+' || ch == '-')
	{
		sign = ch;

		GetChar(inps);		
		GetTerm(inps);

		if (sign == '-')
		{
			num = stk.top();
			stk.pop();
			num = -num;
			stk.push(num);
		}
	}

	GetTerm(inps);
	
	while (ch == '+' || ch == '-')
	{
		termOp = ch;
		
		GetChar(inps);
		GetTerm(inps);
		
		num2 = stk.top();
		stk.pop();
		num1 = stk.top();
		stk.pop();

		if (termOp == '+')
			num = num1 + num2;
		else
			num = num1 - num2;

		stk.push(num);
	}
}

void Expression::GetFactor(fstream& inps)
{
	if (ch >= '0' && ch <= '9')
	{
		string str;

		while (ch >= '0' && ch <= '9' && !inps.eof())
		{
			str += ch;
			ch = (char)inps.get();

			if (ch == ' ' || ch == '\t' || ch == '\n')
				break;

			else if (ch == '^' || ch == '+' || ch == '-' ||
				ch == '*' || ch == '/')
				break;
		}

		if (inps.eof())
			exit(-1);

		while (ch == ' ' || ch == '\t' || ch == '\n')
			ch = (char)inps.get();

		number = atoi(str.c_str());
		stk.push(number);
		return;
	}
	else
	{
		GetChar(inps);
		GetExpression(inps);
		GetChar(inps);
	}
}

void Expression::GetTerm(fstream& inps)
{
	char factOp;
	int num, num1, num2;

	GetFactor(inps);

	while (ch == '*' || ch == '/' || ch == '^')
	{
		factOp = ch;

		GetChar(inps);
		GetFactor(inps);

		num2 = stk.top();
		stk.pop();
		num1 = stk.top();
		stk.pop();

		if (factOp == '*')
			num = num1 * num2;
		else if (factOp == '/')
			num = num1 / num2;
		else if (factOp == '^')
			num = (int)pow(num1, num2);

		stk.push(num);
	}
}
#include "pch.h"
#include "Expression.h"
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    bool validExp;
    int expVal;
    fstream inps;
    Expression ex;

    inps.open("TestExp.txt", fstream::in);
    
    if (!inps.eof())
    {
        validExp = true;
        ex.GetChar(inps);
        ex.GetExpression(inps);
        expVal = ex.stk.top();
        ex.stk.pop();
        cout << "Value = " << expVal << endl;
    }

    else
    {
        validExp = false;
        expVal = 0;
    }

    return 0;
}

C++ Low Density Subset Sum Problem Solver Application Using the L3 Lattice Basis Reduction Algorithm Implemented by James Pate Williams, Jr. Utilizing the “Handbook of Applied Cryptography” Edited by Alfred J. Menezes Among Others

See the website https://cacr.uwaterloo.ca/hac/about/chap3.pdf

Atkin’s Primality Test by James Pate Williams, Jr.

Back in 2022 I reimplemented Henri Cohen’s Atkin’s Primality Test algorithm. This test makes use of an elliptic curve analog of Pocklington’s theorem. I restate the theorem utilized from Henri Cohen’s “A course in Computational Algebraic Number Theory” on pages 467 to 468: “Proposition 9.2.1. Let N be an integer coprime to 6 and different from 1, and E be an elliptic curve modulo N. Assume that we know an integer m a point P contained on the elliptic curve satisfying the following conditions. (1) There exists a prime divisor q of m such that q  >  (N^1/4 + 1) ^ 2 (2) m * P = O_E = (0 : 1 : ). (3) (m / q) * P = (x : y : t) with t contained in (Z/NZ)*. Then N is prime.” I used C# and Microsoft’s BigInteger class. I have not been able to prove numbers greater than 14 decimal digits to be prime. I am recoding the algorithm in C++ which limits me to 19 decimal digits since 2 ^ 63 – 1 = 9,223,372,036,854,775,807 (Int64).

Henri Cohen’s Brent’s Modification of the Pollard rho Factorization Method Implemented by James Pate Williams, Jr.

I implemented the algorithm in C using unsigned 64-bit integers. This method is good for integers of around 18 decimal digits in length. For comparison I tested against my blazingly fast Shor’s classical factoring algorithm which works on arbitrarily large integers of around 50 to 60 or more decimal digits.

Henri Cohen’s Trial Division Algorithm Implemented by James Pate Williams, Jr.

I implemented this algorithm for C long (32-bit) number in 1997, C# big integers in approximately 2015 or 2016, and C unsigned long long (64-bit) integers in March, 2023.

/*
Author:  Pate Williams (c) 1997 - 2023

"Algorithm 8.1.1 (Trial Division). We assume given
a table of prime numbers p[1] = 2, p[2] = 3,..., p[k],
with k > 3, an array t <- [6, 4, 2, 4, 4, 6, 2], and
an index j such that if p[k] mod 30 is equal to
1, 7, 11, 13, 17, 19, 23 or 29 then j is equal to
0, 1, 2, 3, 4, 5, 6 or 7 respectively. Finally, we
give ourselves an upper bound B such that B >= p[k],
essentially to avoid spending too much time. Then
given a positive integer N, this algorithm tries to
factor (or split N) and if it fails, N will be free
of prime factors less than or equal to B."
-Henri Cohen-
See "A Course in Computational Algebraic Number
Theory" by Henri Cohen page 420.
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define BITS_PER_LONG   32
#define BITS_PER_LONG_1 31
#define MAX_SIEVE 100000000L
#define LARGEST_PRIME 99999989L
#define SIEVE_SIZE (MAX_SIEVE / BITS_PER_LONG)

typedef unsigned long long ull;

struct factor { int expon;  ull prime; };
long *prime, sieve[SIEVE_SIZE];

int get_bit(long i, long *sieve) {
	long b = i % BITS_PER_LONG;
	long c = i / BITS_PER_LONG;

	return (sieve[c] >> (BITS_PER_LONG_1 - b)) & 1;
}

void set_bit(long i, long v, long *sieve) {
	long b = i % BITS_PER_LONG;
	long c = i / BITS_PER_LONG;
	long mask = 1 << (BITS_PER_LONG_1 - b);

	if (v == 1)
		sieve[c] |= mask;
	else
		sieve[c] &= ~mask;
}

void Sieve(long n, long *sieve) {
	int c, i, inc;

	set_bit(0, 0, sieve);
	set_bit(1, 0, sieve);
	set_bit(2, 1, sieve);
	for (i = 3; i <= n; i++)
		set_bit(i, i & 1, sieve);
	c = 3;
	do {
		i = c * c, inc = c + c;
		while (i <= n) {
			set_bit(i, 0, sieve);
			i += inc;
		}
		c += 2;
		while (!get_bit(c, sieve)) c++;
	} while (c * c <= n);
}

void get_bits(ull b, int *number, int *bits) {
	int i = 0;
	do {
		bits[i++] = b % 2;
		b = b / 2;
	} while (b > 0);
	*number = i;
}

 ull pow_mod(
	 ull x,
	 ull b, 
	 ull n) {
	// Figure 4.4 in "Cryptography Theory and Practice"
	// by Douglas R. Stinson page 127
	int bits[64], i, l;
	ull z = 1, s = 0;

	get_bits(b, &l, bits);
	s = bits[l - 1];
	for (i = l - 2; i >= 0; i--)
		s = 2 * s + bits[i];
	if (b != s) {
		printf("Error in pow_mod function\n");
		exit(-1);
	}
	for (i = l - 1; i >= 0; i--) {
		z = (z * z) % n;
		if (bits[i] == 1)
			z = (z * x) % n;
	}
	return z;
}

ull rand_ull(void) {
	ull r = 0;
	for (int i = 0; i < 64; i += 15 /*30*/) {
		r = r * ((ull)RAND_MAX + 1) + rand();
	}
	return r;
}

int Miller_Rabin(ull n, int t) {
	// returns 1 for prime
	// returns 0 for composite
	// "Handbook of Applied Cryptography"
	// Alfred J. Menezes among others
	// 4.24 Algorithm page 139
	int i, j, s = 0;
	ull a, n1 = n - 1, r, y;

	r = n1;
	while ((r % 2) == 0) {
		s++;
		r /= 2;
	}
	for (i = 0; i < t; i++)
	{
		while (true)
		{
			a = rand_ull() % n;
			
			if (a >= 2 && a <= n - 2)
				break;
		}
		y = pow_mod(a, r, n);
		if (y != 1 && y != n1) {
			j = 1;
			while (j <= s - 1 && y != n1) {
				y = (y * y) % n;
				if (y == 1)
					return 0;
				j++;
			}
			if (y == n1)
				return 0;
		}
	}
	return 1;
}

int trial_division(ull *N, long *prime,
	struct factor *f, int *count) {
	int found;
	ull B, d;
	int e, i, j = 0, k, m, n;
	int t[8] = { 6, 4, 2, 4, 2, 4, 6, 2 };
	int table[8] = { 1, 7, 11, 13, 17, 19, 23, 29 };
	ull l, r;

	if (*N <= 5) {
		*count = 1;
		f[0].expon = 1;
		switch (*N) {
		case 1: f[0].prime = 1; break;
		case 2: f[0].prime = 2; break;
		case 3: f[0].prime = 3; break;
		case 4: f[0].expon = 2; f[0].prime = 2; break;
		case 5: f[0].prime = 5; break;
		}
		return 1;
	}
	*count = 0;
	B = LARGEST_PRIME;
	i = -1, l = (ull)sqrt((double)*N), m = -1;
L2:
	m++;
	if (i == MAX_SIEVE) {
		i = j - 1;
		goto L5;
	}
	d = prime[m];
	k = d % 30;
	found = 0;
	for (n = 0; !found && n < 8; n++) {
		found = k == table[n];
		if (found) j = n;
	}
L3:
	r = *N % d;
	if (r == 0) {
		e = 0;
		do {
			e++;
			*N /= d;
		} while (*N % d == 0);
		f[*count].expon = e;
		f[*count].prime = d;
		*count = *count + 1;
		if (*N == 1) return 1;
		goto L2;
	}
	if (d >= l) {
		if (*N > 1) {
			f[*count].expon = 1;
			f[*count].prime = *N;
			*count = *count + 1;
			*N = 1;
			return 1;
		}
	}
	else if (i < 0) goto L2;
L5:
	i = (i + 1) % 8;
	d = d + t[i];
	if (d > B) return 0;
	goto L3;
}

int main(void) {
	char e_buffer[256], n_buffer[256] = { 0 };
	long k, e_length, n_length = 0, valid;
	double time_spent;
	int count;
	long i, p = 2;
	ull largest_prime;
	long prime_count = 0;
	ull N, sqrtN;
	struct factor f[32];
	clock_t begin, end;

	begin = clock();
	Sieve(MAX_SIEVE, sieve);
	for (p = 2; p < MAX_SIEVE; p++) {
		while (!get_bit(p, sieve))
			p++;
		prime_count++;
	}
	prime_count--;
	prime = (long*)malloc(prime_count * sizeof(long));
	i = 0;
	p = 2;
	while (i < prime_count)
	{
		while (!get_bit(p, sieve))
			p++;
		prime[i++] = p++;
	}
	largest_prime = LARGEST_PRIME;
	printf("Largest prime = %I64u\n", largest_prime);
	end = clock();
	time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
	printf("Time to create prime number sieve in seconds %lf\n", time_spent);
	for (;;) {
		printf("N = ");
		scanf_s("%s", e_buffer, 256);
		e_length = strlen(e_buffer);
		n_length = 0;
		valid = 1;
		for (k = 0; valid && k < e_length; k++) {
			if (e_buffer[k] >= '0' &&
				e_buffer[k] <= '9' ||
				e_buffer[k] == ',') {
				if (e_buffer[k] != ',') {
					n_buffer[n_length++] = e_buffer[k];
				}
			}
			else {
				valid = 0;
			}
		}
		if (!valid) {
			printf("Invalid character must in set {0, 1, ..., 9, ',')\n");
			continue;
		}
		else {
			n_buffer[n_length] = '\0';
			N = atoll(n_buffer);
			sqrtN = (ull)sqrt((double)N);
			if (sqrtN > largest_prime) {
				printf("Number is too large!\n");
				printf("Square root %I64u must be < %I64u\n", sqrtN, largest_prime);
				continue;
			}
		}
		if (N <= 0) break;
		trial_division(&N, prime, f, &count);
		printf("Non-trivial factors:\n");
		for (i = 0; i < count; i++) {
			printf("%I64u", f[i].prime);
			if (f[i].expon > 1) printf(" ^ %ld\n", f[i].expon);
			else printf("\n");
		}
		if (N != 1)
		{
			if (Miller_Rabin(N, 20) == 1)
				printf("Residue is a prime number\n");
			else
				printf("Last factor is composite\n");
		}
		else
			printf("Number was completely factored\n");
	}
	return 0;
}