i was trying to solve a problem in codechef .when i started writing its code i realised that i needed to covert a integer to string.
i did this
char *instr=itoa(num);
string s=string(instr);
but it is showing error itoa() was not declared in this scope .where i am going wrong.my full code goes like this:-
#include
#include
#include
using namespace std;
int palin(long int num)
{
char *instr=itoa(num);
string s=string(instr);
int flag=0;
for(int i=0,j=s.length();i>i;
while(x==0 || y==0)
{
y=prime(i);
if(y==1)
x=palin(i);
if(x==0 || y==0)
{
i++;
x=0;
y=0;
}
}
cout<<i<<endl;
return 0;
}
```
any help shall be highly appreciated
See if including “#include<bits/stdc++.h” solves the problem or not.
Else refer here - https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
EDIT: itoa isnt available in c++, instead, use to_string() function.
PS: Your prime function has 2 errors, and also check X. Your code has atleast 4-5 bugs which are going to chomp you down in debugging. XD
Here is the code which compiles and runs in C++14, (G 4.9.2)
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int palin(long int num)
{
//char *instr=itoa(num);
string s=to_string(num);
int flag=0;
for(int i=0,j=s.length()-1;i<s.length()/2;i++,j--)
{
if(s[i]!=s[j])
{
flag=1;
break;
}
}
if (flag==1)
return 0;
else return 1;
}
int prime(int j)
{
for(int i=2;i<=j/2;i++){
if(j%i==0)
return 0;
}
return 1;
}
int main()
{
long int i;
int x=0,y=0;
cin>>i;
while(x==0 || y==0)
{
y=prime(i);
if(y==1)
x=palin(i);
if(x==0 || y==0)
{
i++;
x=0;
y=0;
}
}
cout<<i<<endl;
return 0;
}
I also fixed errors in your palin function (It is here - for(int i=0,j=s.length()-1;i<s.length()/2;i++,j--)
Here, j should be as “j=s.length()-1” because j=s.length will throw out of index thing, which causes undefined behavior. Here this caused your function to always return 0 and end up as a TLE )
1 Like
@vijju123 no it doesn’t help.showing same error again.
i already refeered to the link before asking this question .even to_string(num) is not working.
Change it to string s= to_string (num) ; It works, i compiled your code.
@vijju123 i replaced it as u said but to_string(num) giving me same error message how did ur code compile.
@vijju123 thanks for pointing out errors in prime function.corrected them.
Wait, i am uploading a screen shot and the compiled code.
There were errors in string part due to error in prime function and other places. I fixed them as well (thats how i was able to comment that the code has errors )
the reason to_string() was giving me error was that i was compiling my code in c++ 4.9.2(GCC 4.9.2).GOT THE PROGRAM EXECUTED NOW…
prefer C++ 14 because it has features which C++ 4.9.2 doesnt have
@vijju123 when i am taking long value of number(say 462895) i am geeting TLE .how to overcome that.
corrected TLE Bug .thanks for all ur help.
Yes, a TLE is expected for very large number. You can use sieve of erastothenes to find priems upto 10^7 and store them in array/vector and use it.
Trick is that, during sieve you can store the prime numbers in an array like-
int arr[10000];
k=0;
.
.
.
sieve()
if(isPrime(num))
arr[k++]=num
Give me some time, I am caught up atm. I will give you a AC code if you want ^^