Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- int scanf(const char *, ...);
- int printf(const char *, ...);
- int f(unsigned int n);
- /* Количество простых чисел, меньших n
- */
- int f(unsigned int n)
- {
- unsigned int i, j, res = 0;
- char *a;
- a = (char *)malloc(n*sizeof(char));
- if(a==0) {
- free(a);
- return -1;
- }
- for(i = 0; i<n; i++) a[i] = 1;
- for(i = 2; i*i<n; i++)
- {
- if(a[i]==1)
- for(j = i; j<=(unsigned int)(n/i); j++) a[j*i] = 0;
- }
- //for(i = 0; i<n; i++) printf("%d\t%d\n", i, a[i]);
- for(i = 2; i<n; i++)
- res += a[i];
- free(a);
- return res;
- }
- int main(void)
- {
- unsigned int n;
- printf("n=");
- if( scanf("%u", &n)==0 )
- {
- printf("Input error!\n");
- return 1;
- }
- printf("Result: %d\n", f(n));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement