Skip to content

Instantly share code, notes, and snippets.

@JustinChristensen
Forked from ichaleynbin/gist:5131918
Created March 11, 2013 04:39
Show Gist options
  • Save JustinChristensen/5131924 to your computer and use it in GitHub Desktop.
Save JustinChristensen/5131924 to your computer and use it in GitHub Desktop.
# include <iostream>
# include <cstdio>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <array>
using namespace std;
float logBase2(float N)
{
return log(N)/log(2);
};
void prep (bool arr[],int N,int maxDig) {
int i;
for (i=maxDig;i>=0;i--) {
int base = pow(2,i);
if ((N-base)>=0) {
N = N-base;
arr[i] = true;
} else {
arr[i] = false;}
}
};
class FactoringProblem {
int Target,maxPow,halfMax;
bool * binTarget, * Factor1, * Factor2;
int * Carries, CarriesIn, CarriesOut;
public:
FactoringProblem () {
cout << "What number would you like to factor?\n";
cin >>Target;
maxPow= floor(logBase2(Target));
binTarget = new bool[maxPow];
halfMax = floor(maxPow/2.0);
prep(binTarget,Target,maxPow);
Factor1 = new bool[maxPow];
Factor2 = new bool[halfMax];
unordered_map<const char*, bool> Carries;
unordered_map<int, int> CarriesIn;
unordered_map<int, int> CarriesOut;
int carry0[1] {0};
CarriesOut[0] = carry0[0];
CarriesOut[halfMax + maxPow] = carry0[0];
int i, j,terms;
int maxCarryTerms,baseCounty = 0;
//int caryIn;
maxCarryTerms = int(ceil(logBase2(maxPow)));
for (i=1;i<=halfMax+maxPow+1;i++) {
//int caryIn[maxCarryTerms+1];
//caryIn = new int;
CarriesIn[i] = *(new int[maxCarryTerms+1]);
CarriesIn[i][&baseCounty] = 0;
cout<<CarriesIn[i]<<"SPACE"<<i<<endl;
}
int DisCounty,county, * CaryOut;
int * LastOne;
for (i=0;i<=halfMax+maxPow;i++) {
county = 0;
terms = min(halfMax,min(i,(halfMax + maxPow - i))) + CarriesIn[i][&baseCounty]+1;
CaryOut = new int (ceil(logBase2(terms)));
CarriesOut[i] = *CaryOut;
int valer = int(ceil(logBase2(terms)));
if (valer <1)
{
if (i > 0)
valer = 1;
else
valer = 0;
}
//cout <<"MEOW"<<CarriesIn[i][&baseCounty]<<endl;
//CarriesIn[i][&county] = 0;
//county += 1;
//for (j=i-1;j>=(i-valer);j--) {
// CarriesIn[i][&county+1] = j;
// CarriesIn[i][&baseCounty]+= 1;
// county += 1;
// }
county = 0;
for (j=i+1;j<=min((i+logBase2(terms)),float(maxPow+halfMax));j++) {
int DisCounty, *reDisCounty;
CarriesOut[i][&county] = j;
cout<<CarriesIn[j][&baseCounty]<<endl;
reDisCounty = &CarriesIn[j];
cout <<reDisCounty<<endl;
cout<<&CarriesIn[j]<<endl;
DisCounty = reDisCounty[baseCounty] +1;
cout <<DisCounty<<" "<<reDisCounty[baseCounty]<<" "<<i<<endl;
reDisCounty[DisCounty] = i;
CarriesIn[j][&baseCounty] = DisCounty;
cout <<"Carrying "<<i<<" into "<<j<<endl;
//CarriesIn[j][&baseCounty] += 1;
//cout<<endl;
county += 1;
}
for (j=0;j<=halfMax+maxPow;j++){
cout <<terms<<"ME "<<i<<" OW"<<CarriesIn[j][&baseCounty]<<endl;
cout <<sizeof CarriesOut[j]<<endl;
}
}
prep(Factor1,11,maxPow);
prep(Factor2,11,halfMax);
}
void sF1 () {
cout << "Factor 1: ";
binDisp(Factor1,maxPow);
}
void sF2 () {
cout << "Factor 2: ";
binDisp(Factor2,halfMax);
}
void sT () {
cout << "Target: ";
binDisp(binTarget,maxPow);
}
void vCs() {
int i,j,terms;
for (i=0;i<=halfMax;i++) {
//terms = min(halfMax,min(i,(halfMax + maxPow - i))) + (sizeof CarriesIn[i] / sizeof (int)) -1;
//for (j=i-1;j>=(i-logBase2(terms));j--) {
// cout<<CarriesIn[i][j]<<endl;
// }
// for (j=i+1;j<=min((i+logBase2(terms)),float(maxPow+halfMax));j++) {
// cout<<CarriesOut[i][j]<<endl;
// }
//cout <<&Carries[i]<<endl;
//binDisp(Carries[i],maxPow);//for (j=0;j<=maxPow;j++) {
}
}
void binDisp(bool arr[],int maxPow) {
int i;
for (i=maxPow;i>=0;i--)
{
cout <<arr[i];
}
cout <<endl;
}
int sumAtDigit(int N) {
int sumit=0, i;
for (i=0;i<=min(halfMax,N);i++)
{
sumit += Factor2[i]*Factor1[N-i];
}
//switch(sumit) {
// case(2<=sumit<4) {
//cout <<"lolz"<<endl;
//}
// }
return sumit;
}
};
int main()
{
FactoringProblem Problem;
Problem.sF1();
Problem.sF2();
Problem.sT();
Problem.vCs();
cout <<Problem.sumAtDigit(1)<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment