c - Sieve of Eratosthenes and his primes -
this code:
#include <stdio.h> int main() { int number; int prime[200000] = { 0 }; int = 0; int j = 0; int number1[200] = { 0 }; int t = 0; int count = 0; int newprime2[200][200]; int counter[200] = { 0 }; int square; int count1; while ((scanf("%d", &number) == 1 ) && (number != 0)) { number1[count] = number; ++count; } count1 = count; (count = 0; count < count1; ++count) { if (number1[count] < 0) { fprintf(stderr, "error: invalid input!\n"); return 100; break; } (i = 0; < number1[count]; i++) { prime[i] = i; } (i = 2; (i < (number1[count])); i++) { if (prime[i] != 0) { (j = 2; (j < (number1[count])); j++) { { prime[j*prime[i]] = 0; if (prime[i] * j > (number1[count])) break; } } } } t = 0; (i = 2; < number1[count]; ++i) { if ((prime[i] != 0) && (number1[count] % prime[i] == 0)) { newprime2[count][t] = prime[i]; ++t; } } printf("\n"); printf("%i made out of these primes\n", number1[count]); counter[count] = 0; square = 0; (i = 0; < t; ++i) { while (number1[count] % newprime2[count][i] == 0) { number1[count] = number1[count] / newprime2[count][i]; square++; } counter[count]++; /* if number isn't made out of of these primes*/ if (!newprime2[count][i]) { /*why not working?*/ printf("%i ", number1[count]); } if (counter[count] == 1) { printf("%i^%d ", newprime2[count][i], square); } else { printf("* %i^%d ", newprime2[count][i], square); } square = 0; } } printf("\n"); return 0; }
for example, input is: 1 11 120 8 0
output looks this:
1 made out of these primes 11 made out of these primes 120 made out of these primes 2^3 * 3^1 * 5^1 8 made out of these primes 2^3
but output should looks this:
1 made out of these primes 1 11 made out of these primes 11 ...
statement (!newprime2[count][i])
means array empty right? why isn't working? , why can't use gcc -pedantic -wall -werror -std=c99 -o3
? can me?
see part of code:
t = 0; (i = 2; < number1[count]; ++i){ if ((prime[i]!=0) && (number1[count] % prime[i]==0)){ newprime2[count][t] = prime[i]; ++t; }
if number1[count]
1
, body of for
loops will not execute, sot
keep value (0
). consequently body of next loop
(i=0; < t; ++i){
will not execute, too.
for number 11
body of loop will execute nothing condition in if
statement always false
. results same problem - t
keep value 0
same consequence.
Comments
Post a Comment