Semiprime

Jun 26, 2013 at 2:11pm
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++ ;
    }
  }
}

Jun 27, 2013 at 2:56am
Please help !!!!!!!!!!!!
Jun 27, 2013 at 3:25am
You might begin by removing the semi-colon on line 34.
Last edited on Jun 27, 2013 at 3:34am
Jun 27, 2013 at 3:27am
ok ty that was dumb from my side .
but it still displays 8, 12 which arent semiprimes
Last edited on Jun 27, 2013 at 3:27am
Jun 27, 2013 at 3:34am
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.
Jun 27, 2013 at 3:34am
What does isprime(4) return?
Jun 27, 2013 at 4:00am
if i<x then false, but if i<x/2 then it will return true.
Jun 27, 2013 at 4:18am
isprime(4) should return false but it returns true
Last edited on Jun 27, 2013 at 4:30am
Jun 27, 2013 at 4:20am
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 Jun 27, 2013 at 4:25am
Jun 27, 2013 at 4:32am
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++ ;
    }
  }

}
Jun 27, 2013 at 4:35am
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().
Jun 27, 2013 at 4:41am
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?
Jun 27, 2013 at 4:54am
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?
Jun 27, 2013 at 6:00am
yup it almost halves the number of iterations
Jun 27, 2013 at 6:14am
do help me with the palindrome program i just posted its quite .. frustrating...
thanks..
Topic archived. No new replies allowed.