Semiprime

i had to make a program to print first 25 semiprimes i.e numbers which can be expressed as product of two primes (distinct or indistinct).
My code seems fine but the output is not right whats the snag ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

bool isprime(int x)
{
   for(int i = 2; i<x/2 ;i++)
    { if(x%i==0)
      {  return false ;
    }
  }
  return true ;
}

bool isSemiprime(int x)
{
    for(int i = 2 ; i < x/2 ; i++ )
    {
           if(x%i==0)
            {
                if(isprime(i)&&isprime(x/i))
               {
                  return true ;
                  }
           }
    }
    return false ;
}

int main()
{   int cnt=0 ;
    for(int i = 4 ;cnt<25;i++)
    {
      if(isSemiprime(i)) ;
      { cout<< i << endl;
        cnt++ ;
    }
  }
}

Please help !!!!!!!!!!!!
You might begin by removing the semi-colon on line 34.
Last edited on
ok ty that was dumb from my side .
but it still displays 8, 12 which arent semiprimes
Last edited on
1
2
3
bool isprime(int x)
{
   for(int i = 2; i<x/2 ;i++)


Why i<x/2 ?

Should be i<x, but still doesn't print the first semiprime 4.
What does isprime(4) return?
if i<x then false, but if i<x/2 then it will return true.
isprime(4) should return false but it returns true
Last edited on
for checking if a number is prime we need to only check if it is divisible by a number upto x/2 more precisely upto sqrt x would do .
this is done to increase the efficiency of the program

oops that s hould have been i<=x/2 but still it doesnt print 4
Last edited on
Thank guys figured it out know here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

bool isprime(int x)
{
   for(int i = 2; i <= x/2 ;i++)
    { if(x%i==0)
      {  return false ;
    }
  }
  return true ;
}

bool isSemiprime(int x)
{
    for(int i = 2 ; i <= x/2 ; i++ )
    {
           if(x%i==0)
            {
                if(isprime(i)&&isprime(x/i))
               {
                  return true ;
                  }
           }
    }
    return false ;
}

int main()
{   int cnt=0 ;
    for(int i = 3 ;cnt<25;i++)
    {
      if(isSemiprime(i))
      { cout<< i << endl;
        cnt++ ;
    }
  }

}
oops that s hould have been i<=x/2 but still it doesnt print 4


You mean it should have been i<x.

If it's i<=x/2 then isprime(4) will be true, which should return false.

Why do you think it still doesn't print 4?

I think there's also something wrong on isSemiprime().
If it's i<=x/2 then isprime(4) will be true, which should return false.

It would return false.

Why do you think it still doesn't print 4?

Because you're not scrolling up far enough in your console to see it?
my bad, i thought it's still i<x/2..

But it works too if i<x on both isprime and isSemiprime.

So i<=x/2 is for more efficient?
yup it almost halves the number of iterations
do help me with the palindrome program i just posted its quite .. frustrating...
thanks..
Topic archived. No new replies allowed.