Lychrel Numbers

#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

Advertisement

~ by sinea on June 7, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.