Whats wrong in the code its not passing any of the testcases

hello @sneha23

i think the issue is with ur fact function, u r checking for prime number right?
but there can be cb number as well that are non prime for example
37*37 (it is not prime but it is cb number)
so correct it and then try to submit again

but i have already returned 1 for all other cases ie after 29
isnt??

no …check output of fact for 31*31 .
it is giving 0

actually its giving this compilation error in code
so i m not able to figure what is the problem can u pls tell why is it giving this error
/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

this error occurs when we try to access invalid array index(negative inde or index greater than equal size of array)

#include
#include
using namespace std;
int fact( int num)
{
if(num<=1)
{
return 0;
}
if(num<=3)
{
return 1;
}
if(num%3==0||num%2==0)
{
return 0;
}
for(int i=5;i*i<num;i=i+6)
if(num%(i)==0||num%(i+2)==0)
{
return 0;
}
else
return 1;
}
bool isvisited(int v,int i,int j)
{
for(;i<=j;i++){
if(v[i]==1)
return true;
}
return false;
}
int main()
{
int n;
cin>>n;
string s;
cin>>s;
int c=0;
int v[n]={0};
for(int i=0;i<n;i++)
{
for(int j=0;i<n-i;j++)
{
int num=0;
int temp=j;
while(temp<=i+j)
{
num=num
10+(s[temp]-‘0’);
temp++;
}
if(fact(num)==1)
{
if(!isvisited(v,j,j+i))
{
c++;
for(int k=j;k<=j+i;k++)
v[k]=1;
}
}
}
}
cout<<c;
return 0;
}
im not able figure out the probllem…pls resolve if possible

ok i will check , but first change ur code of fact function it is wrong.

and share ur updated code link

in fact function i have almost counted for all cases
pls check this

once run this code ->

correct answer should be 1 but urs is giving 0.

ur fact function should be like this->
image

now pls make appropriate changes in ur code , and share the updated link with me

hey,i knew this way but i wnted toknow my fact func is not working i dont see anythng wrong in it after all the cases i m returning 1 for rest but while running it is showing it as 0.
can you make me understnd wht is the problem behind

@sneha23
ur fact fact function is checking for prime number right?
if it is prime then it will return 1 otherwise 0.

now it is true that all prime numbers are cb number , so ur code will work fine for prime number.
but not all cb numbers are prime number hence ur code will for some non prime cases.

some example->
31* 31,37* 37, 43* 43,47* 47 and there are so many

BUT FOR ELSE I M RETURNING 1 WHY IS IT NOT TAKING THAT

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.