Integer Sequence Explorer

My integer sequence database explorer is finally up and running over at my website.  Essentially, this application allows the user to choose two integer sequences from many common sequences, then runs a query which selects common members from each sequence.

One of the interesting tasks has been to write many small applications which produce the sequences to be loaded into the database relations.  I started out a little too optimistic, and wrote code which calculated the first million primes, squares, sum of divisors, and others.  I quickly found that when I ran queries on these tables, they took way to long, and my server was timing out.  I incrementally scaled things back until I was left with the first 10,000 members of most sequences.  Now my task is to re-design the database to allow for larger searches.

I will be loading up more sequences over the next few weeks, and posting explanations and links for the sequences.

Object Oriented Factorization Program in PHP

I love actuarial stuff.  But, when I am learning any kind of new programming or mathematics, I always think number theory.

On my deathbed, I will probably not be thinking about loss ratios.  But I will be thinking about divisibility.  Number theory is the thing that originally drew me, and many others, to mathematics.

So, in the interest of learning PHP, I wanted to put a factorization application up on my website. But I wanted to be able to factorize more than one number at a time, so that one can observe the relationship between properties of different integers. So, I thought, what I want is several instances of the factorizer. That means writing an object oriented application.
You will observe below that the constructor does all of the work. That is because number theory stuff is very calculation intensive. Many of the variables, like phi, can be calculated as by-products of other calculations.

class DestructNumber
{ var $N;
private $sumofdivisors = 0;
private $divisorlist=array();
private $primedivisorlist=array();
private $primedivisorstring;
private $phi = 1;
// this constructor does all of the work
// that allows the utility to use a more efficient
// algorithm than a strictly obect oriented approach
// would allow

public function __construct($N) { $this->number = $N;
// find half of divisors
$counter = 1;
$end = floor(sqrt($this->number));
for ($m=1; $m <= $end;$m++ ) { if ($this->number%$m == 0)
{$this->divisorlist[$counter]=$m;
$this->sumofdivisors=$this->sumofdivisors + $m;
$counter++;
}
}
//find other half of divisors
for( $m = $counter-1;$m > 0;$m--)
{
$quotient = $this->number/$this->divisorlist[$m];
if ($this->divisorlist[$m] != $quotient)
{$this->divisorlist[$counter] = $quotient;
$this->sumofdivisors=$this->sumofdivisors + $quotient;
$counter++;
}}


// extract primes
$bar=1;
for($m = 2; $m <= $counter-1; $m++) { if (DestructNumber::primep($this->divisorlist[$m]))
{$this->primedivisorlist[$bar][1]=$this->divisorlist[$m];
$bar++;}
}
//find powers of primes
for ($m=1; $m <= $bar-1; $m++) { $power=1; $x = round(pow($this->primedivisorlist[$m][1], $power));

while ($this->number % $x == 0
&& $x <= $this->number)
{
$power++;
$x = round(pow($this->primedivisorlist[$m][1], $power));
}
$this->primedivisorlist[$m][2] = $power-1;
}
// create prime divisor string
$this->primedivisorstring =””;
for ($m = 1; $m <= $bar-1;$m++) { $this->primedivisorstring .= $this->primedivisorlist[$m][1];
if ($this->primedivisorlist[$m][2]>1 )
{ $this->primedivisorstring .= ““.$this->primedivisorlist[$m][2].”“;
}
if ($m < $bar-1) {$this->primedivisorstring .= “*”;}
$this->phi = $this->phi * round(pow($this->primedivisorlist[$m][1], ($this->primedivisorlist[$m][2]-1)))*($this->primedivisorlist[$m][1]-1);
}
}

All of the work has been done by my constructor. Now, I just need some methods to return the values.

// define methods
private function primep ($N){
$stop = floor(sqrt ($N));
for ( $m=2; $m <= $stop; $m++) { if ($N%$m ==0) return 0; } return 1; } public function get_N(){ return $this->number;
}
public function get_sigma(){
return $this->sumofdivisors-$this->number;
}
public function get_numberdivisors(){
return count($this->divisorlist);
}
public function get_divisorstring(){
return $this->primedivisorstring;
}
public function get_phi(){
return $this->phi;
}
public function get_prime(){
return (count($this->divisorlist) == 2 ) ? "Yes" : "No";
}
public function get_perfect(){
return ($this->sumofdivisors == $this->number) ? "Yes" : "No";
}
public function get_square(){
return ( count($this->divisorlist)%2 == 1) ? "Yes" : "No";
}

}

There are some fun things here. A number is prime if it has two divisors. A number is square if it has an odd number of divisors.
Here is the link for my web factorizer.

Next, I will be posting a database mining application to explore number properties.

A Simple Windows Application for Factorizing Integers

So, I spend so much time working and doing mathematics and walking the dogs that I don’t always spend much time on programming. But, over the last year, I have realized that everything that I need to make windows applications is already on my computer. I don’t need any fancy software: just a text editing program, and an open source compiler to compile from the command line.
So my first code that I am posting here is a C++ application for factorizing integers. It just runs in a window with no fancy GUI. But it factorizes numbers very nicely.

The link for the downloadable exe file is below the code.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdint>
#include <string>
//  #include <NTL/ZZ.h>  get this number theory stuff eventually

bool primep (int64_t N);

int main()
{
using namespace std;
std::string another ="N";
do
{
int64_t N;
int64_t divisorlist[1000];
int64_t primedivisorlist[100][3];
int64_t sumofdivisors =0;
int64_t phi=1;
int counter (1);
bool squarep (false);
cout<< "This application factors integers of up to seventeen digits."<<endl;
cout << "Enter an integer to be factored: " <<endl;
cin>>N;
cin.ignore(32767, '\n');

for (int m=1; m<= floor(sqrt(N));m++ )
{
if (N%m == 0)
{divisorlist[counter]=m;
counter++;}
}

for(int m=counter-1;m> 0;m--)
{
int64_t quotient = N/divisorlist[m];
if (divisorlist[m] != quotient)
{divisorlist[counter]=quotient;
counter++;
}}
// now extract primes
int bar=1;
for(int m =2; m<=counter-1;m++)
{
if (primep(divisorlist[m]))
{primedivisorlist[bar][1]=divisorlist[m];
bar++;}
}
// now find powers of primes

for (int m=1; m<=bar-1; m++)
{
int power=1;
int64_t x = round(pow(primedivisorlist[m][1], power));

while (N % x ==0
&& x<=N)
{
power++;
x = round(pow(primedivisorlist[m][1], power));
}
primedivisorlist[m][2]=power-1;
}
cout<<N<<" = ";
for(int m =1; m<=bar-1;m++)

{
cout<<primedivisorlist[m][1];
if (primedivisorlist[m][2]>1 )
cout<< "^"<< primedivisorlist[m][2];
if (m<bar-1) cout<<"*";
phi=phi*round(pow(primedivisorlist[m][1], (primedivisorlist[m][2]-1)))*(primedivisorlist[m][1]-1);
}
cout<<endl;

cout<< "Number of divisors: "<<counter-1<<endl;
cout<<"The divisors are: "<<endl;
for(int m =1; m<=counter-1;m++)
{
cout<<divisorlist[m]<<" ";
sumofdivisors = sumofdivisors+divisorlist[m];
}
cout<<endl;
cout<<"The sum of the aliquot divisors of "<<N<<" is "<<sumofdivisors-N<<". Hence, this number is ";
if (sumofdivisors >(2*N)) {cout<<"abundant. "<<endl;}
else if (sumofdivisors<(2*N)){cout<<"deficient. "<<endl;}
else {cout<<"perfect. "<<endl;}

cout<<"Phi, the number of integers less than and relatively prime to "<<N<<" is "<<phi<<endl;
cout<<N<<" is ";
if (primep (N))
cout<<" ";
else cout<<"not ";
cout<<"prime."<<endl;
squarep = (counter-1)%2;
cout<<N<<" is ";
if (squarep)
cout<<" ";
else cout<<"not ";
cout<<"a perfect square."<<endl;
cout<<"Another number? ";

std::getline(std::cin, another);
}
while ((another == "yes") || (another == "y"));

return 0;
}

bool primep (int64_t N)

//returns t if N is prime
{
for (int m=2; m<= floor(sqrt (N)); m++)
{
if (N%m ==0) return false;
}
return 1;
}

The code is very simple and self-explanatory.  It is not the most efficient and beautiful way to factorize a number, but it works well enough within the realm of integers that can be easily handled by the base C++ variable types.

Here is the link for the exe file:

Factorizer