# Thread: Help me with my basic program, nothing I create will run

1. It looks like your algorithm almost works, except that you are off by one prime, because 3 is not listed as a prime. To see why 3 is left out, consider what happens when test_n = 2. First, it is printed, then test_n=3 and divisor=2. Now, test_n != divisor, so test_n=4 and divisior=2. On the next iteration, 3 has been skipped entirely.

This may be a more typical algorithm for trial division:
Code:
```if number is 2
number is prime
else if number is divisible by 2
number is composite
else
calculate square root of number
loop over the odd integers starting from 3 until the square root calculated
if number is divisible by current odd integer
number is composite (can end loop)
if loop ends without finding a divisor
number is prime```

2. My program seems to be nearly right... But still doesn't give me all of the first 100 primes (although it does seem to list only primes, haven't checked all though).

Any ideas why this isn't working...
by the way, my trash has 76 copies of a.out and they're all attempts at this silly program...

so any help would be greatly appreciated.
I like your logic statements, but I dislike the way they're joined together. It's sort of like a magician. He's NOT actually doing magic, but it seems like he might be (or is), because of the artful way he misdirects your eye, so you can't see what he's really doing, even though he's right in front of you.

That's what you code does for me - it misdirects me just enough to miss some logic actions that we need.

I've tried to leave out some syntax items you'll need to add, and I haven't run this program yet, so expect some bugs.

Your previous program was the "I sat down at the keyboard and typed up some code w/o having worked out how to find prime numbers, step by step, by hand, first".

This program I wrote is the same - and dangerous because I don't work with square roots and prime's, and haven't written a program to do this in a VERY long time.

When you craft code on paper first, you get the quality product you're capable of. When you sit down and pound keys first, you get only as good as your previous knowledge extends, and little improvement.

Humans are very lazy and find efficient ways to do repetitive work with the tools they can get: Quicksort is a fine example.

Computers are very dumb, and will happily do a Bubblesort on a 10million records sized database, on a regular basis.

/* program to find first 100 prime numbers */
Code:
```#include <stdio.h>
#include <math.h>  /* for square root function */
int main(void)
{

int counter, test_n, divisor;
counter = 100;
divisor = 3;
printf("\n\n")
for (test_n = 1; test_n < 4; test_n++)
printf("Our prime numbers are &#37;7d ", test_n)

while (counter)

{

max = sqr(test_n)       /* this is the max number we have to test for primeness */
while (divisor < max)   /* main inner working loop */
{
if (test_n % divisor == 0) {
is_prime = 0  /* test_n is not prime */
break
}
else
is_prime = 1  /* OK, maybe it is a prime after all */

divisor = divisor +1;  /* this should be changed to improve the program. How'd you change it? */
}

if (is_prime) {  /* it's a prime number */
counter--       /* print the prime and continue countdown to 100 primes  */
printf("Our prime numbers are %7d ", test_n)
}

/* reset divisor and is_prime, increment test_n,  */
divisor = 3
test_n++
is_prime = 0

}

return 0;
}```

Popular pages Recent additions