Lychrel Numbers

•June 7, 2009 • Leave a Comment
#include <iostream>
#include <fstream>
using namespace std;
bool isPoli(int* arr, int length)
{
for(int i=0; i<length/2; i++)
if(arr[i] != arr[length-i-1])
return false;
return true;
}
bool isLishel(int* numArray, int length, int depth)
{
/* IF I REACH DEPTH OF 100 JUST LEAVE IT … 
* … CAN CHANGE TO ANY DEPTH … JUST NEED 
* TO HAVE VERY FAST CPU
*/
if(++depth == 100) return false;
int rest = 0;
int *fin = new int[length+1];
for(int i = 0 ; i < length; i ++)
{
fin[i] = (numArray[i]+numArray[length-i-1]+rest)%10;
rest = (numArray[i]+numArray[length-i-1]+rest)/10;
}
int l = length;
if(rest != 0)
{
l++;
fin[length] = rest;
}
if(isPoli(fin, l))
return true;
if(isLishel(fin, l, depth))
return true;
return false;
}
int main()
{
int num, *arr;
int num2 = 0;
int length = 0;
arr = new int[30];
int *pre = new int[32768];
int idx = 0;
int numberOfNumbers = 0, startIndex, finIndex;
memset(pre, 0, 32768);
ifstream from;
ofstream output;
output.open(“c29i.out”);
from.open(“c29i.in”);
from >> numberOfNumbers;
cout << “I read ” << numberOfNumbers << ” pairs of numbers …” << endl;
system(“pause”);
cout << “Starting program ” << endl;
int gasite = 0;
for(int k=0; k<numberOfNumbers; k++)
{
from >> startIndex;
from >> finIndex;
for(int i=startIndex ; i<finIndex ; i++)
{
num = i;
if(pre[i] == 2)
gasite++;
else if(pre[i] == 0)
{
memset(arr, 0, 30);
while(num!= 0)
{
arr[length++] = num%10;
num/=10;
}
if(!isLishel(arr, length, 0))
{
pre[i] = 2;
gasite++;
}
else
{
pre[i] = 1;
}
length = 0;
}
}
output << k << “: ” << “found between ” << startIndex << ” and ” << finIndex << ” : ” << gasite <<endl;
gasite = 0;
}
cout << “Complete…” << endl;
return 0;
}

 Here (http://www.skullbox.info/concursuri-de-programare/(incepatori)(concurs-nr-29)numere-lychrel/)  is a contest … the problem sounds like this:

- read N from “c29i.in” where 1<= N <= 10000;

-read the N pairs of numbers from ”c29i.in” wich are something like  … 0<a<b<2^15

-must output into “c29i.out” the number of Lychrel numbers between every pair of numbers [a, b] … the numbers are considered Lychrel if they do not turn into a polindrome after 100 aditions with himself reversed … (1239 + 9321)

 

mode about Lychrel numbers .. http://en.wikipedia.org/wiki/Lychrel_number

#include <iostream>

#include <fstream>

 

using namespace std;

 

bool isPoli(int* arr, int length)

{

for(int i=0; i<length/2; i++)

if(arr[i] != arr[length-i-1])

return false;

return true;

}

 

bool isLishel(int* numArray, int length, int depth)

{

/* IF I REACH DEPTH OF 100 JUST LEAVE IT … 

* … CAN CHANGE TO ANY DEPTH … JUST NEED 

* TO HAVE VERY FAST CPU

*/

if(++depth == 100) return false;

int rest = 0;

int *fin = new int[length+1];

 

for(int i = 0 ; i < length; i ++)

{

fin[i] = (numArray[i]+numArray[length-i-1]+rest)%10;

rest = (numArray[i]+numArray[length-i-1]+rest)/10;

}

int l = length;

if(rest != 0)

{

l++;

fin[length] = rest;

}

if(isPoli(fin, l))

return true;

 

if(isLishel(fin, l, depth))

return true;

 

return false;

}

 

int main()

{

int num, *arr;

int num2 = 0;

int length = 0;

 

arr = new int[30];

int *pre = new int[32768];

int idx = 0;

int numberOfNumbers = 0, startIndex, finIndex;

memset(pre, 0, 32768);

ifstream from;

ofstream output;

 

output.open(“c29i.out”);

from.open(“c29i.in”);

 

from >> numberOfNumbers;

cout << “I read ” << numberOfNumbers << ” pairs of numbers …” << endl;

system(“pause”);

cout << “Starting program ” << endl;

int gasite = 0;

for(int k=0; k<numberOfNumbers; k++)

{

from >> startIndex;

from >> finIndex;

for(int i=startIndex ; i<finIndex ; i++)

{

num = i;

if(pre[i] == 2)

gasite++;

else if(pre[i] == 0)

{

memset(arr, 0, 30);

while(num!= 0)

{

arr[length++] = num%10;

num/=10;

}

 

if(!isLishel(arr, length, 0))

{

pre[i] = 2;

gasite++;

}

else

{

pre[i] = 1;

}

length = 0;

}

}

output << k << “: ” << “found between ” << startIndex << ” and ” << finIndex << ” : ” << gasite <<endl;

gasite = 0;

}

 

cout << “Complete…” << endl;

return 0;

}

 

this code 2-3 days ago did not cracked … today it seems that it dies right after writing all the numbers to the “c29i.out” file :|

 

yes and another thing … here is a sample of  ”c29i.in” file

(3 lines folowed by 3 pairs [a, b])

10  10000

123  34512

5127 15000

my very own Array class -> like iterator pattern :)

•May 17, 2009 • Leave a Comment

Hello i have implemented a Array class with wich i(and you) can make an array of objects(any object), automaticaly search it for a certain object, send objects from inside an array to another. For now it’s very simple beacause i don’t have time to write one with much more useful things… hopefully will help someone. I made this at seing the list class … i don’t like it … just looks so stiff  :| . Hhere’s the code .. :)

#include <iostream>
using namespace std;
/* Array class writen by … (just visit sinea.wordpress.com)
 * this has to be something like list in c++
 * but much easier to use by overloading some
 * operators .. 
 * TODO :   – extend functionality
 * [- get a life :) ) ] 
 */
template <class T>
class Array
{
T* objects;
bool fixedLength;
int numberOfObjects;
public:
Array(int length = 0, bool fix = false)
{
fixedLength = fix;
if(length != 0)
objects = new T[length];
else
objects = new T[];
numberOfObjects = 0;
};
//ADD NEW  T ITEM AT THE END OF THE ARRAY AND RETURN IT
T& push(T object)
{
objects[numberOfObjects++] = object;
return objects[0];
}
//REMOVE THE LAST OBJECT OF THE ARRAY AND RETURN THE NEW LAST ONE
T& pop()
{
objects[numberOfObjects] = NULL;
numberOfObjects–;
return objects[numberOfObjects];
}
//SWITCH 2 OBJECTS IN THE ARRAY RETURN BOOL ACCORDINGLY(SUCCES OR NOT)
bool switchObjects(int firstIndex, int secondIndex)
{
if(firstIndex >= numberOfObjects || secondIndex >= numberOfObjects)
throw “WTF IS WITH THAT INDEX :O “;
try
{
T temp = objects[firstIndex];
objects[firstIndex] = objects[secondIndex];
objects[secondIndex] = temp;
}
catch(…)
{
return false;
}
return true;
}
//FIND OUT THE INDEX OF A OBJECT STARTING FROM THE GIVEN INDEX AND RETURN -1 IF THE OBJECT DOES NOT EXIST
int idexOf(T object, int starIndex = 0)
{
for(int i = starIndex;  i < numberOfObjects; i ++)
if(objects[i] == object)
return i;
return -1;
}
//RETURN THE CURENT LENGTH OF THE ARRAY
int Length() const
{
return numberOfObjects;
}
//CLEAN THE ARRAY AND RETURN BOOL(SUCCES OR FAILURE)
bool Clean()
{
try
{
for(int i = 0;  i < numberOfObjects; i ++)
objects[i] = NULL;
numberOfObjects = 0;
}
catch(…)
{
return false;
}
return true;
}
//EASY RETURN THE ITEM FROM A ARRAY BY USING THE [] OPERATOR(OVERLOADED)
T& operator[](int index)
{
if(index >= numberOfObjects)
throw “WTF IS WITH THAT INDEX :O “;
return objects[index];
}
// USE THIS IF WE WANT TO CONCATENATE 2 ARRAYS
bool operator+=(Array& from)
{
for(int i = 0; i < from.Length() ; i ++)
{
push(from[i]);
}
return true;
}
// USE IF WE WANT THIS ARRAY TO SEND THE OBJECTS AT THE INDEXS SPECIFIED IN THE elements[]
void SendElements(Array& to, int elements[], int nElements)
{
for(int i = 0; i < nElements; i++)
to.push(objects[elements[i]]);
}
};
int main()
{
Array<int> arr;
arr.push(10);
arr.push(101);
arr.push(12);
Array<int> aray2;
aray2.push(88);
aray2.push(99);
for(int i=0; i<arr.Length(); i++)
cout << arr[i] << ” “;
cout << endl;
arr.switchObjects(1, 2);
for(int i=0; i<arr.Length(); i++)
cout << arr[i] << ” “;
cout << endl;
arr += aray2;
int elem[] = {0, 1};
aray2.SendElements(arr, elem, 2);
aray2 = arr;
cout << “aray2 “;
for(int i=0; i<aray2.Length(); i++)
cout << aray2[i] << ” “;
cout << endl;
cout << “arr “;
for(int i=0; i<arr.Length(); i++)
cout << arr[i] << ” “;
cout << endl;
return 0;
}

#include <iostream>

using namespace std;

 

/* Array class writen by … (just visit sinea.wordpress.com)

 * this has to be something like list in c++

 * but much easier to use by overloading some

 * operators … and adding some fancy functionality

 * TODO :   – extend functionality

 * [- get a life :) ) ] 

 */

template <class T>

class Array

{

T* objects;

bool fixedLength; // ACTUALLY DOES NOT HELP WITH ANYTHING :P

int numberOfObjects;

public:

Array(int length = 0, bool fix = false)

{

fixedLength = fix;

if(length != 0)

objects = new T[length];

else

objects = new T[];

numberOfObjects = 0;

};

//ADD NEW  T ITEM AT THE END OF THE ARRAY AND RETURN IT

T& push(T object)

{

objects[numberOfObjects++] = object;

return objects[0];

}

//REMOVE THE LAST OBJECT OF THE ARRAY AND RETURN THE NEW LAST ONE

T& pop()

{

objects[numberOfObjects] = NULL;

numberOfObjects–;

return objects[numberOfObjects];

}

//SWITCH 2 OBJECTS IN THE ARRAY RETURN BOOL ACCORDINGLY(SUCCES OR NOT)

bool switchObjects(int firstIndex, int secondIndex)

{

if(firstIndex >= numberOfObjects || secondIndex >= numberOfObjects)

throw “WTF IS WITH THAT INDEX :O “;

try

{

T temp = objects[firstIndex];

objects[firstIndex] = objects[secondIndex];

objects[secondIndex] = temp;

}

catch(…)

{

return false;

}

return true;

}

//FIND OUT THE INDEX OF A OBJECT STARTING FROM THE GIVEN INDEX AND RETURN -1 IF THE OBJECT DOES NOT EXIST

int idexOf(T object, int starIndex = 0)

{

for(int i = starIndex;  i < numberOfObjects; i ++)

if(objects[i] == object)

return i;

return -1;

}

//RETURN THE CURENT LENGTH OF THE ARRAY

int Length() const

{

return numberOfObjects;

}

//CLEAN THE ARRAY AND RETURN BOOL(SUCCES OR FAILURE)

bool Clean()

{

try

{

for(int i = 0;  i < numberOfObjects; i ++)

objects[i] = NULL;

numberOfObjects = 0;

}

catch(…)

{

return false;

}

return true;

}

//EASY RETURN THE ITEM FROM A ARRAY BY USING THE [] OPERATOR(OVERLOADED)

T& operator[](int index)

{

if(index >= numberOfObjects)

throw “WTF IS WITH THAT INDEX :O “;

return objects[index];

}

// USE THIS IF WE WANT TO CONCATENATE 2 ARRAYS

bool operator+=(Array& from)

{

for(int i = 0; i < from.Length() ; i ++)

{

push(from[i]);

}

return true;

}

// USE IF WE WANT THIS ARRAY TO SEND THE OBJECTS AT THE INDEXS SPECIFIED IN THE elements[]

void SendElements(Array& to, int elements[], int nElements)

{

for(int i = 0; i < nElements; i++)

to.push(objects[elements[i]]);

}

};

 

int main()

{

Array<int> arr;

arr.push(10);

arr.push(101);

arr.push(12);

Array<int> aray2;

aray2.push(88);

aray2.push(99);

for(int i=0; i<arr.Length(); i++)

cout << arr[i] << ” “;

cout << endl;

arr.switchObjects(1, 2);

for(int i=0; i<arr.Length(); i++)

cout << arr[i] << ” “;

cout << endl;

 

arr += aray2;

int elem[] = {0, 1};

 

aray2.SendElements(arr, elem, 2);

 

aray2 = arr;

cout << “aray2 “;

for(int i=0; i<aray2.Length(); i++)

cout << aray2[i] << ” “;

cout << endl;

 

cout << “arr “;

for(int i=0; i<arr.Length(); i++)

cout << arr[i] << ” “;

cout << endl;

 

return 0;

}

The Singleton design pattern

•May 16, 2009 • Leave a Comment

Hy there, here’s a singleton class(SoundManager). Using a singleton design pattern is very useful, at least that’s what i use in game developement.  This design pattern make your life easy beacause it has only one instance.. again .. ONLY ONE  :)  threfore you can call the static member function getInstance() to obtain the single instance of the class from anywhere in the code … enaugh said .. the rest is code :)

made just to prove the singleton pattern

 

#include <iostream>
using namespace std;
class Sound{};
class SoundManager
{
static SoundManager * instance;
static int numberOfInstances;
Sound * sounds;
int numberOfSounds;
SoundManager()
sounds = new Sound[]; // DYNAMIC ARRAY OF SOUNDS
numberOfSounds = 0;
};
public:
~SoundManager(){ numberOfInstances — ; /*IF WE DESTROY A INSTANCE OF THE SINGLETON WITH delete() WE MUST DECREMENT THE NUMBER OF INSTANCES*/};
static SoundManager * getInstance();
void addSound(Sound sound);
int howManySounds();
int howManyInstances();
};
int SoundManager::numberOfInstances = 0; // INITIALIZE THE NUMBER OF INSTANCES WITH 0
SoundManager* SoundManager::instance = NULL; // MAKE THE INITIAL INSTANCE NULL
SoundManager * SoundManager::getInstance()
{
if(numberOfInstances == 0) // IF THE NUMBER OF INSTANCES IS 0 (NONE HAS BEEN MADE YET)  … 
instance = new SoundManager(); // WE SHOULD MAKE A NEW ONE … RIGHT ? :)
numberOfInstances ++ ; // INCREMENT THE INSTANCE COUNTER
return instance;
}
// .. THE REST IS HISTORY
void SoundManager::addSound(Sound sound)
{
cout << ” – added sound – ” << endl;
sounds[numberOfSounds++] = sound;
}
int SoundManager::howManySounds()
{
return numberOfSounds;
}
int SoundManager::howManyInstances()
{
return numberOfInstances;
}
int main()
{
SoundManager *s1, *s2;
Sound s,a,b,c;
s1 = SoundManager::getInstance();
s2 = SoundManager::getInstance();
s1->addSound(s);
s1->addSound(a);
s1->addSound(b);
s1->addSound(c);
cout << ” No instances : ” << s1->howManyInstances() << endl; // WE NOW HAVE JUST 2 INSTANCES OF TRHE OBJECT BUT ONLY ONE OBJECT
cout << ” No sounds : ” << s2->howManySounds() << endl; // WE HAVE 4 SOUNDS
cout << ” No sounds : ” << s1->howManySounds() << endl; // STIL 4 SOUNDS BEACAUSE THE S1 POINTS AT THE SAME INSTANCE AS S2
// LET’S MAKE ANOTHER INSTANCE … BUT NOT A NEW OBJECT …
SoundManager *lastInstance = SoundManager::getInstance();
cout << ” No sounds : ” << lastInstance->howManySounds() << endl; // YEP STIL 4 SOUNDS :)
cout << ” No instances : ” << lastInstance->howManyInstances() << endl;
return 0;
}

#include <iostream>

using namespace std;

class Sound{};

 

class SoundManager

{

static SoundManager * instance;

static int numberOfInstances;

Sound * sounds;

int numberOfSounds;

SoundManager()

sounds = new Sound[]; // DYNAMIC ARRAY OF SOUNDS

numberOfSounds = 0;

};

public:

~SoundManager(){ numberOfInstances — ; /*IF WE DESTROY A REFERENCE TO THE SINGLETON WITH delete() WE MUST DECREMENT THE NUMBER OF REFERENCE */};

static SoundManager * getInstance();

void addSound(Sound sound);

int howManySounds();

int howManyInstances();

};

 

int SoundManager::numberOfInstances = 0; // INITIALIZE THE NUMBER OF INSTANCES WITH 0

SoundManager* SoundManager::instance = NULL; // MAKE THE INITIAL INSTANCE NULL

 

SoundManager * SoundManager::getInstance()

{

if(numberOfInstances == 0) // IF THE NUMBER OF REFS IS 0 (NONE HAS BEEN MADE YET)  … 

instance = new SoundManager(); // WE SHOULD MAKE A NEW ONE … RIGHT ? :)

numberOfInstances ++ ; // INCREMENT THE REFS COUNTER

return instance;

}

// .. THE REST IS HISTORY

void SoundManager::addSound(Sound sound)

{

cout << ” – added sound – ” << endl;

sounds[numberOfSounds++] = sound;

}

 

int SoundManager::howManySounds()

{

return numberOfSounds;

}

 

int SoundManager::howManyInstances()

{

return numberOfInstances;

}

int main()

{

SoundManager *s1, *s2;

Sound s,a,b,c;

s1 = SoundManager::getInstance();

s2 = SoundManager::getInstance();

s1->addSound(s);

s1->addSound(a);

s1->addSound(b);

s1->addSound(c);

 

cout << ” No instances : ” << s1->howManyInstances() << endl; // WE NOW HAVE JUST 2 POINTERS TO THE OBJECT BUT ONLY ONE OBJECT

 

cout << ” No sounds : ” << s2->howManySounds() << endl; // WE HAVE 4 SOUNDS

cout << ” No sounds : ” << s1->howManySounds() << endl; // STIL 4 SOUNDS BEACAUSE THE S1 POINTS AT THE SAME INSTANCE AS S2

 

// LET’S MAKE ANOTHER POINTER AT THE SINGLETON CLASS… BUT NOT A NEW OBJECT …

SoundManager *lastInstance = SoundManager::getInstance();

cout << ” No sounds : ” << lastInstance->howManySounds() << endl; // YEP STIL 4 SOUNDS :)

 

cout << ” No instances : ” << lastInstance->howManyInstances() << endl;

 

return 0;

}

Node class … can make a tree of strings and search the whole tree for a certain term

•May 14, 2009 • Leave a Comment

 

#include <iostream>
using namespace std;
class Node
{
char * m_value;
int m_children;
Node* children;
public:
Node();
void Text(char* text);
char* Text() const;
void AddChild(Node& nod);
bool GetNode(Node& to, char* like);
};
Node::Node()
{
m_children = 0;
children = new Node[];
cout << “BUILD” << endl;
}
void Node::Text(char *text)
{
m_value = text;
cout << “SET VALUE : ” << m_value << endl;
}
char* Node::Text() const
{
return m_value;
}
void Node::AddChild(Node& nod)
{
children[m_children] = nod;
m_children++;
}
bool Node::GetNode(Node &to, char *like)
{
if(m_value == like)
{
to = *this;
return true;
}
for(int i=0; i<m_children; i++)
if(children[i].GetNode(to, like))
return true;
return false;
}
int main()
{
Node n;
n.Text(“www”);
Node a;
a.Text(“bubu”);
Node b;
b.Text(“qwertyKeyboard”);
Node c;
c.Text(“apple”);
Node aa;
aa.Text(“sunny”);
Node bb;
bb.Text(“NAN”);
Node cc;
cc.Text(“Smiley”);
a.AddChild(aa);
a.AddChild(bb);
a.AddChild(cc);
n.AddChild(a);
n.AddChild(b);
n.AddChild(c);
Node final;
if(a.GetNode(final, “alina”))
{
cout << final.Text() << endl;
}
return 0;
}

who knows .. maybe usefull sometime :)

#include <iostream>

using namespace std;

class Node

{

char * m_value;

int m_children;

Node* children;

public:

Node();

void Text(char* text);

char* Text() const;

void AddChild(Node& nod);

bool GetNode(Node& to, char* like);

};

Node::Node()

{

m_children = 0;

children = new Node[];

cout << “BUILD” << endl;

}

void Node::Text(char *text)

{

m_value = text;

cout << “SET VALUE : ” << m_value << endl;

}

char* Node::Text() const

{

return m_value;

}

void Node::AddChild(Node& nod)

{

children[m_children] = nod;

m_children++;

}

bool Node::GetNode(Node &to, char *like)

{

if(m_value == like)

{

to = *this;

return true;

}

 

for(int i=0; i<m_children; i++)

if(children[i].GetNode(to, like))

return true;

 

return false;

}

 

int main()

{

Node n;

n.Text(“www”);

Node a;

a.Text(“bubu”);

Node b;

b.Text(“qwertyKeyboard”);

Node c;

c.Text(“apple”);

 

Node aa;

aa.Text(“sunny”);

Node bb;

bb.Text(“NAN”);

Node cc;

cc.Text(“Smiley”);

 

a.AddChild(aa);

a.AddChild(bb);

a.AddChild(cc);

 

n.AddChild(a);

n.AddChild(b);

n.AddChild(c);

 

Node final;

if(a.GetNode(final, “someTerm”))

{

cout << final.Text() << endl;

}

return 0;

}

MyInt class – small to demonstrate operator overloading

•May 12, 2009 • Leave a Comment

 

class MyInt
{
int m_number;
public:
MyInt(int number)
{
m_number = number;
cout << “NEW MYINT MADE. VALUE : ” << m_number << endl;
};
/* GETER FOR M_NUMBER */
int value() const
{
return m_number;
}
/* SHOW THE VALUE OF THE CONTAINED INTEGER */
void showValue() const
{
cout << m_number << endl;
}
/* OVERLOAD THE + OPERATOR FOR THIS CLASS 
* MAKE THE SUM OF THE TWO MEMBER VARS
* AND RETURN A NEW OBJECT             */
MyInt operator+(MyInt& rightValue)
{
/* MAKE NEW MyInt OBJECT INITIALIZED 
* WITH THE SUM OF THE 2 MEMBER INT’s 
* rightValue IS THE ONE ON THE RIGHT 
* OF THE + OPERATOR :p*/
MyInt rV(m_number + rightValue.value());
/* RETURN THE NEW OBJECT */
return rV;
}
MyInt operator*(MyInt& rightValue)
{
/* EXACTLY THE SAME AS THE + OPERATOR */
MyInt rV(m_number * rightValue.value());
/* RETURN THE NEW OBJECT */
return rV;
}
/* MAKING THE < OPERATOR FOR OUR CLASS */
bool operator<(MyInt& rightValue)
{
/* SAME AS + AND * THE rightValue IS THE 
* OBJECT FROM THE RIGHT OF THE OPERATOR
* AND THE ONE FROM THE LEFT IS THE 
* CURENT(this) ONE*/
cout << m_number << ” < ” << rightValue.value() << endl;
return m_number < rightValue.value();
}
/* MAKING THE > OPERATOR FOR OUR CLASS */
bool operator>(MyInt& rightValue)
{
/* SAME AS + AND * THE rightValue IS THE 
* OBJECT FROM THE RIGHT OF THE OPERATOR
* AND THE ONE FROM THE LEFT IS THE 
* CURENT(this) ONE*/
cout << m_number << ” > ” << rightValue.value() << endl;
return m_number > rightValue.value();
}
~MyInt(){};
};

Basic operator overloading in c++ for a class (MyInt in this case :p)

class MyInt

{

private:

int m_number;

public:

MyInt(int number)

{

m_number = number;

cout << “NEW MYINT MADE. VALUE : ” << m_number << endl;

};

/* GETER FOR M_NUMBER */

int value() const

{

return m_number;

}

/* SHOW THE VALUE OF THE CONTAINED INTEGER */

void showValue() const

{

cout << m_number << endl;

}

/* OVERLOAD THE + OPERATOR FOR THIS CLASS 

* MAKE THE SUM OF THE TWO MEMBER VARS

* AND RETURN A NEW OBJECT             */

MyInt operator+(MyInt& rightValue)

{

/* MAKE NEW MyInt OBJECT INITIALIZED 

* WITH THE SUM OF THE 2 MEMBER INT’s 

* rightValue IS THE ONE ON THE RIGHT 

* OF THE + OPERATOR :p*/

MyInt rV(m_number + rightValue.value());

/* RETURN THE NEW OBJECT */

return rV;

}

 

MyInt operator*(MyInt& rightValue)

{

/* EXACTLY THE SAME AS THE + OPERATOR */

MyInt rV(m_number * rightValue.value());

/* RETURN THE NEW OBJECT */

return rV;

}

/* MAKING THE < OPERATOR FOR OUR CLASS */

bool operator<(MyInt& rightValue)

{

/* SAME AS + AND * THE rightValue IS THE 

* OBJECT FROM THE RIGHT OF THE OPERATOR

* AND THE ONE FROM THE LEFT IS THE 

* CURENT(this) ONE*/

cout << m_number << ” < ” << rightValue.value() << endl;

return m_number < rightValue.value();

}

/* MAKING THE > OPERATOR FOR OUR CLASS */

bool operator>(MyInt& rightValue)

{

/* SAME AS + AND * THE rightValue IS THE 

* OBJECT FROM THE RIGHT OF THE OPERATOR

* AND THE ONE FROM THE LEFT IS THE 

* CURENT(this) ONE*/

cout << m_number << ” > ” << rightValue.value() << endl;

return m_number > rightValue.value();

}

~MyInt(){};

};

Goldbach’s conjecture made simple in c++

•May 11, 2009 • Leave a Comment

 

#include <iostream.h>
#include <math.h>
#include <time.h>
bool isPrime(int& x)
{
        if (x == 1 || (x % 2 == 0 && x != 2)) return false;
if(x % 3 == 0) return false;
        for (int i = 3; i <= sqrt(x); i += 2)
            if (x % i == 0)
                return false;
        return true;
}
int main()
{
cout << ” — GOLDBACH — ” << endl;
int numar,ultim;
bool gasit = false;
cin >> numar;
clock_t startTime = clock();
clock_t finTime;
ultim = numar;
if(numar < 4)
return 0;
for(int i=0; i<=numar/2; i++)
{
if(isPrime(i))
{
for(int j = ultim-1; j >= numar/2; j-=2)
{
if(isPrime(j) && i+j == numar)
{
cout << “First : ” << i << endl << “Second : ” << j << endl;
i = numar;
j = 0;
}
else if(i+j < numar)
{
ultim = j+1;
break;
}
}
}
}
finTime = clock();
cout << finTime << ” -> ” << startTime << “ms” << endl;
cout << ” – - – - – - – ” << endl;
return 0;
}
Goldbach for numbers between 4 and 2000000000 :)
These are the number of ways to write an even number n as the sum of two primes (4 ≤ n ≤ 1,000,000);
Runs in less than 1 second
Enjoy
#include <iostream.h>
#include <math.h>
#include <time.h>
bool isPrime(int& x)
{
        if (x == 1 || (x % 2 == 0 && x != 2)) return false;
if(x % 3 == 0) return false;
        for (int i = 3; i <= sqrt(x); i += 2)
            if (x % i == 0)
                return false;
        return true;
}
int main()
{
cout << ” — GOLDBACH — ” << endl;
int numar,ultim;
bool gasit = false;
cin >> numar;
clock_t startTime = clock();
clock_t finTime;
ultim = numar;
if(numar < 4)
return 0;
for(int i=0; i<=numar/2; i++)
{
if(isPrime(i))
{
for(int j = ultim-1; j >= numar/2; j-=2)
{
if(isPrime(j) && i+j == numar)
{
cout << “First : ” << i << endl << “Second : ” << j << endl;
i = numar;
j = 0;
}
else if(i+j < numar)
{
ultim = j+1;
break;
}
}
}
}
finTime = clock();
cout << finTime << ” -> ” << startTime << “ms” << endl;
cout << ” – - – - – - – ” << endl;
return 0;
}

c++ recursive function for files

•May 10, 2009 • Leave a Comment

Using a recursive function in c++ to search a file or folder using dirent.h :)

void renderFolder(char folder[])
{
DIR *director = opendir(folder);

if(director)
{
struct dirent *deCitit;
deCitit= readdir(director);
char *nume;

while (deCitit=readdir(director))
{
nume = deCitit->d_name;
char newPath[512];

/* 

USE indicatorExtensie TO VERIFY IF EXTENSION IS .exe IN THIS CASE OR WE COULD SEARCH THE NAME OF THE FILE 
*/

char * indicatorExtensie;
indicatorExtensie = strstr(nume,”.exe”);

strcpy(newPath, folder);
strcat(newPath , “\\”);
strcat(newPath , nume);

/** NOW WE HAVE THE PATH TO THE FILE IN  newPath **/

if(indicatorExtensie)
/** DO SOMETHING WITH  newPath …. ANYTHING  :) **/

/** CONTINUE THE SEARCH :P   **/
renderFolder(newPath);
}
}
}

Cu asta incep …

•May 10, 2009 • Leave a Comment

Primul post … sant curios cat o sa scriu pe blogu’ asta … nu ii dau mai mult de 2 saptamani .. :) )

Hello world!

•May 10, 2009 • Leave a Comment

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

 
Follow

Get every new post delivered to your Inbox.