Why my code is not working

can you identify and correct it ?

It’ s prime fact using sieve

@premang corrected your code for producing prime factors less than n


dont forget to mark it as resolved and hit like if cleared :smiley:

1 Like

i dont understand this part
int i=1;
//int prime=p[0];
while(i*i<=n) {
//factors.push_back(prime);
if(p[i]==1 && n%i==0) {
factors.push_back(i);
}
i++;
}

why we are not starting from p[0] ?
what the meaning of this (p[i]==1 && n%i==0) ?

@premang hey the prime sieve you built denotes if ith value is prime or not so i am iterating on factors of n and if prime[i] = 1 means if the factor is prime and it is factor(n%i==0) push it and continue

1 Like

i dont understand ur logic,
we made array p which is having all prime numbers.

then we should check the condition (primeprime <= number)
then why are you doing (i
i<=number), i is index which is starting from 1.
1*1 is always <= number

just tell me why we are starting from i=1 ?

@premang you want factors right thats why also your prime array contains 1 or 0 only denoting ele present or not

1 Like

just dry run it for n=12

1 Like

i the code you edited , i entered n=10
no output why ?

@premang o/p is coming 2 for n=10

1 Like

got it
thankssssssssssssssssssss :wink:

np just dont forget to hit like and mark it as resolved @premang :smiley:

1 Like

i already marked as solved

ok thanks @premang :smiley:

1 Like