Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bugs in an is_prime() implementation

Reply
Thread Tools

Bugs in an is_prime() implementation

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      01-24-2007
Hello experts,

The following is_prime function doesn't call a library function and it
works.

Does it have bugs like "integer overflow": int factor; factor * factor,
or it's not single entry/exit, or others... Thank you.

/*return 0 if num is a prime number otherwise 1*/
bool is_prime(int num)
{
int factor;
if (num < 2) return 1;
for (factor = 2; factor * factor <= num; factor++)
if (num % factor == 0)
return 1;
return 0;
}

#include <stdio.h>
int main(void)
{
for (int i = 0; i < 1000; i++)
if (is_prime(i))
printf("%d", i) ;
return 0;
}

/*
Run: C:\MinGW\bin\mingw32-make.exe
gcc -ansi -pedantic -Wall -W -c -o a.o a.cpp
gcc a.o b.o -o a.out

Press the Enter key to return to Source Insight...

Run: D:\working\c\a.out
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 1
07 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
199 211 2
23 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
317 331 3
37 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439
443 449 4
57 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571
577 587 5
93 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691
701 709 7
19 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829
839 853 8
57 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977
983 991 9
97
Press the Enter key to return to Source Insight...
*/

 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      01-24-2007
(E-Mail Removed) wrote:
> [C++ code]


This group deals with C, not C++. Go ask this in comp.lang.c++.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-24-2007
Harald van Dijk wrote:
> (E-Mail Removed) wrote:
>
>>[C++ code]

>
>
> This group deals with C, not C++. Go ask this in comp.lang.c++.
>

Excuse me, but with the addition of an include of <stdbool.h> the code
is legal C.

--
Ian Collins.
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      01-24-2007
Ian Collins wrote:
> Harald van Dijk wrote:
> > (E-Mail Removed) wrote:
> >
> >>[C++ code]

> >
> >
> > This group deals with C, not C++. Go ask this in comp.lang.c++.
> >

> Excuse me, but with the addition of an include of <stdbool.h> the code
> is legal C.


The code can be modified to become valid C99. The code as is is valid
C++ (with different behaviour than claimed by the OP) and compiled in
C++ mode.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-24-2007
(E-Mail Removed) wrote:
> Hello experts,
>
> The following is_prime function doesn't call a library function and it
> works.
>
> Does it have bugs like "integer overflow": int factor; factor * factor,
> or it's not single entry/exit, or others... Thank you.
>
> /*return 0 if num is a prime number otherwise 1*/
> bool is_prime(int num)
> {
> int factor;
> if (num < 2) return 1;
> for (factor = 2; factor * factor <= num; factor++)
> if (num % factor == 0)
> return 1;
> return 0;
> }
>

Lots of mixed idioms, if you are using C99, use true and false, not 0 and 1.

You logic appears to be arse about face, returning 1 if the number has a
factor.

> #include <stdio.h>
> int main(void)
> {
> for (int i = 0; i < 1000; i++)
> if (is_prime(i))
> printf("%d", i) ;
> return 0;
> }
>
> /*
>
> Run: D:\working\c\a.out
> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
> 101 103 1
> 07 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
> 199 211 2
> 23 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
> 317 331 3
> 37 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439
> 443 449 4
> 57 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571
> 577 587 5
> 93 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691
> 701 709 7
> 19 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829
> 839 853 8
> 57 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977
> 983 991 9
> 97
> Press the Enter key to return to Source Insight...
> */
>

This can't be the output of the above, your printf doesn't include any
spaces and the logic is wrong.

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-24-2007
Ian Collins said:

> Harald van D?k wrote:
>> (E-Mail Removed) wrote:
>>
>>>[C++ code]

>>
>>
>> This group deals with C, not C++. Go ask this in comp.lang.c++.
>>

> Excuse me, but with the addition of an include of <stdbool.h> the code
> is legal C.


True, although it's a legal C for which hardly anyone has a conforming
compiler. Nevertheless, the way he's compiling it indicates pretty clearly
that he thinks he's using C++. Why else would he name his source file
a.cpp?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-24-2007
Harald van Dijk wrote:
> Ian Collins wrote:
>
>>Harald van Dijk wrote:
>>
>>>(E-Mail Removed) wrote:
>>>
>>>
>>>>[C++ code]
>>>
>>>
>>>This group deals with C, not C++. Go ask this in comp.lang.c++.
>>>

>>
>>Excuse me, but with the addition of an include of <stdbool.h> the code
>>is legal C.

>
>
> The code can be modified to become valid C99. The code as is is valid
> C++ (with different behaviour than claimed by the OP) and compiled in
> C++ mode.
>

Leaving out header files in posted snippets is a common mistake.

--
Ian Collins.
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      01-24-2007
Ian Collins wrote:
> Harald van Dijk wrote:
> > Ian Collins wrote:
> >
> >>Harald van Dijk wrote:
> >>
> >>>(E-Mail Removed) wrote:
> >>>
> >>>
> >>>>[C++ code]
> >>>
> >>>
> >>>This group deals with C, not C++. Go ask this in comp.lang.c++.
> >>>
> >>
> >>Excuse me, but with the addition of an include of <stdbool.h> the code
> >>is legal C.

> >
> >
> > The code can be modified to become valid C99. The code as is is valid
> > C++ (with different behaviour than claimed by the OP) and compiled in
> > C++ mode.
> >

> Leaving out header files in posted snippets is a common mistake.


He didn't leave out <stdio.h>, and adding <stdbool.h> would make it
invalid C++ (which, again, he's compiling his code as).

Another point: if he compiled it with the given compiler options as C
code, gcc would attempt to conform to C90, which does not allow
<stdbool.h> and declarations in for statements.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-24-2007
Harald van Dijk wrote:
> Ian Collins wrote:
>>
>>Leaving out header files in posted snippets is a common mistake.

>
>
> He didn't leave out <stdio.h>, and adding <stdbool.h> would make it
> invalid C++ (which, again, he's compiling his code as).
>
> Another point: if he compiled it with the given compiler options as C
> code, gcc would attempt to conform to C90, which does not allow
> <stdbool.h> and declarations in for statements.
>

OK, OK. I didn't spot the compile line, I just read the (what appeared
to be C99) code.

--
Ian Collins.
 
Reply With Quote
 
Kelly
Guest
Posts: n/a
 
      01-24-2007
(E-Mail Removed) wrote:
> Hello experts,
>
> The following is_prime function doesn't call a library function and it
> works.
>
> Does it have bugs like "integer overflow": int factor; factor * factor,
> or it's not single entry/exit, or others... Thank you.
>
> /*return 0 if num is a prime number otherwise 1*/
> bool is_prime(int num)
> {
> int factor;
> if (num < 2) return 1;
> for (factor = 2; factor * factor <= num; factor++)
> if (num % factor == 0)
> return 1;
> return 0;
> }
>
> #include <stdio.h>
> int main(void)
> {
> for (int i = 0; i < 1000; i++)
> if (is_prime(i))
> printf("%d", i) ;
> return 0;
> }
>

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>Compiling...
1>test.c
1>c\:visual studio 2005\projects\test\test\test.c(3) : error C2061:
syntax error : identifier 'is_prime'
1>c\:visual studio 2005\projects\test\test\test.c(3) : error C2059:
syntax error : ';'
1>c\:visual studio 2005\projects\test\test\test.c(3) : error C2059:
syntax error : 'type'
1>c\:visual studio 2005\projects\test\test\test.c(15) : error C2143:
syntax error : missing ';' before 'type'
1>c\:visual studio 2005\projects\test\test\test.c(15) : error C2143:
syntax error : missing ';' before 'type'
1>c\:visual studio 2005\projects\test\test\test.c(15) : error C2143:
syntax error : missing ')' before 'type'
1>c\:visual studio 2005\projects\test\test\test.c(15) : error C2143:
syntax error : missing ';' before 'type'
1>c\:visual studio 2005\projects\test\test\test.c(15) : error C2065: 'i'
: undeclared identifier
1>c\:visual studio 2005\projects\test\test\test.c(15) : warning C4552:
'<' : operator has no effect; expected operator with side-effect
1>c\:visual studio 2005\projects\test\test\test.c(15) : error C2059:
syntax error : ')'
1>c\:visual studio 2005\projects\test\test\test.c(16) : error C2143:
syntax error : missing ';' before 'if'
1>c\:visual studio 2005\projects\test\test\test.c(16) : warning C4013:
'is_prime' undefined; assuming extern returning int
1>Test - 10 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

on changing the return type of is_prime to int and a further
change(declaration) in main ..i.e

int main(void)
{
int i; /*declared here*/
for (i=0;i<1000;i++)
if (is_prime(i))
printf("%d ",i);
return 0;
}

here is the output:-
0 1 4 6 8 9 10 12 14 15 16 18 20 21 22 24 25 26 27 28 30 32 33 34 35 36
38 39 40 42 44 45 46 48 49 50 51 52 54 55 56 57 58 60 62 63 64 65 66 68
69 70 72 74 75 76 77 78 80 81 82 84 85 86 87 88 90 91 92 93 94 95 96 98
99 100
...the rest of the output snipped...

so basically it just doesn't work!
Is this something to do with the compiler..i.e. the "bool" return type
is addressed differently on differently on different compilers??

This is what happens if i let the return type as bool!

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>Compiling...
1>test.c
1>c:\visual studio 2005\projects\test\test\test.c(3) : error C2061:
syntax error : identifier 'is_prime'
1>c:\visual studio 2005\projects\test\test\test.c(3) : error C2059:
syntax error : ';'
1>c:\visual studio 2005\projects\test\test\test.c(3) : error C2059:
syntax error : 'type'
1>c:\visual studio 2005\projects\test\test\test.c(17) : warning C4013:
'is_prime' undefined; assuming extern returning int
1>Test - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Knowing the implementation, are all undefined behaviours become implementation-defined behaviours? Michael Tsang C Programming 54 03-30-2010 07:46 AM
Knowing the implementation, are all undefined behaviours become implementation-defined behaviours? Michael Tsang C++ 32 03-01-2010 09:15 PM
Bugs and Bugs...get rid of them Jason Computer Security 1 01-31-2006 10:47 PM
Still use 'ruby-bugs' for Ruby bugs? Josef 'Jupp' Schugt Ruby 2 11-04-2004 10:10 PM
Front Controller Implementation in ASP.NET is full of BUGS =?Utf-8?B?TkdN?= ASP .Net 1 01-25-2004 06:16 PM



Advertisments