anyway to make this while statment shorter?

learning C at the momment using sams teach yourself C. finished week one and I tried writing a program to find the first 100 prime numbers.
as you can see I ended up writing a rediculiously long while condition to see if there wasn't a modulus between the variable numb and any listing in the array ar[100]. how can a write a while condition that is more compact but follows the same logical routine as what I have so far?

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
42
43
44
#include <stdio.h>

long long ar[100];
int findprime_main(void);
int main (void)
{
    printf("\ngood morning sean.");
    printf("\nprogram initalizing");
    findprime_main();
    printf("\nprogram complete");
    return 0;
}
int findprime_main()
{
    int ctr2;
    long long numb;
    ar[0]=2;
    ar[1]=3;
    ar[99]=0;
    for(ctr2=2;ar[99]==0;ctr2++)
            {
            ar[ctr2]=2;
            }
    ctr2=2;
    while(ctr2<=99)
            {
            numb=1+(ar[ctr2-1]);
            while(!(numb%ar[0])||!(numb%ar[1])||!(numb%ar[2])||!(numb%ar[3])||!(numb%ar[4])||!(numb%ar[5])||!(numb%ar[6])||!(numb%ar[7])||!(numb%ar[8])||!(numb%ar[9])||!(numb%ar[10])||!(numb%ar[11])||!(numb%ar[12])||!(numb%ar[13])||!(numb%ar[14])||!(numb%ar[15])||!(numb%ar[16])||!(numb%ar[17])||!(numb%ar[18])||!(numb%ar[19])||!(numb%ar[20])||!(numb%ar[21])||!(numb%ar[22])||!(numb%ar[23])||!(numb%ar[24])||!(numb%ar[25])||!(numb%ar[26])||!(numb%ar[27])||!(numb%ar[28])||!(numb%ar[29])||!(numb%ar[30])||!(numb%ar[31])||!(numb%ar[32])||!(numb%ar[33])||!(numb%ar[34])||!(numb%ar[35])||!(numb%ar[36])||!(numb%ar[37])||!(numb%ar[38])||!(numb%ar[39])||!(numb%ar[40])||!(numb%ar[41])||!(numb%ar[42])||!(numb%ar[43])||!(numb%ar[44])||!(numb%ar[45])||!(numb%ar[46])||!(numb%ar[47])||!(numb%ar[48])||!(numb%ar[49])||!(numb%ar[50])||!(numb%ar[51])||!(numb%ar[52])||!(numb%ar[53])||!(numb%ar[54])||!(numb%ar[55])||!(numb%ar[56])||!(numb%ar[57])||!(numb%ar[58])||!(numb%ar[59])||!(numb%ar[60])||!(numb%ar[61])||!(numb%ar[62])||!(numb%ar[63])||!(numb%ar[64])||!(numb%ar[65])||!(numb%ar[66])||!(numb%ar[67])||!(numb%ar[68])||!(numb%ar[69])||!(numb%ar[70])||!(numb%ar[71])||!(numb%ar[72])||!(numb%ar[73])||!(numb%ar[74])||!(numb%ar[75])||!(numb%ar[76])||!(numb%ar[77])||!(numb%ar[78])||!(numb%ar[79])||!(numb%ar[80])||!(numb%ar[81])||!(numb%ar[82])||!(numb%ar[83])||!(numb%ar[84])||!(numb%ar[85])||!(numb%ar[86])||!(numb%ar[87])||!(numb%ar[88])||!(numb%ar[89])||!(numb%ar[90])||!(numb%ar[91])||!(numb%ar[92])||!(numb%ar[93])||!(numb%ar[94])||!(numb%ar[95])||!(numb%ar[96])||!(numb%ar[97])||!(numb%ar[98])||!(numb%ar[99]))
                  {
                  ++numb;
                  }
            ar[ctr2]=numb;
            ++ctr2;
            }      
    ctr2=0;
    while(ctr2<=99)
            {
            printf("\n%d: %d",ctr2+1, ar[ctr2]);
            ctr2++;
            }
}
    
    
    
and is the code listed between lines 20- 23 required? figured that i would get errors with !(numb%0)||, didn't try running the program without it though.
Lines 27-31:
1
2
3
for (numb=1+(ar[ctr2-1]);!divisible;numb++)
    for (i=0;i<100 && !divisible;i++)
        divisible=!(numb%ar[i]);


EDIT:
is the code listed between lines 20- 23 required?
Yes, it is, but it could be better written:
1
2
for (i=0;i<100;i++)
    ar[i]=2;

Isn't that a lot clearer?
Last edited on
! that makes since thanks. Not sure why I didn't think of something like that in the first place. also makes it easy to expand out the array.
gah, it's not working right. going to take a break and will look at it latter. right now the out put is just printing out numbers 2- 101.
seems like the hard part about programing ins't the syntax or keywords, but understanding the cold uncomprimising logic of things.
Welcome. We hope you enjoy your stay.
yea, thats not working. There are other logical ways of finding prime numbers, but when following this logical path I don't think there is a easy way around it. I think it would be easier to just write a program to write the condition if you needed to work with a large array, like ar[5000] or something. I wonder how big a condtion can be, i guess your only inhibited by system memory.

verbatum the code sugested enters into a infinite loop reguardless of the initial state of divisable. when I tinkered with it I could get it too count up incrementaly 2- 102, then 2, 5, 7 8,9,10..... 100andsomething.
I think it would be easier to just write a program to write the condition if you needed to work with a large array, like ar[5000] or something.
No. What if you have no idea at compile time how long the array will be? It's a much more common situation.

i guess your only inhibited by system memory.
You're limited by how long the compiler will feel like compiling your condition. At one point, it'll just go "fuck you, this is too long".
you can replace this part:
1
2
3
4
while(!(numb%ar[0])||!(numb%ar[1])||!(numb%ar[2])||!(numb%ar[3])||!(numb%ar[4])||!(numb%ar[5])||!(numb%ar[6])||!(numb%ar[7])||!(numb%ar[8])||!(numb%ar[9])||!(numb%ar[10])||!(numb%ar[11])||!(numb%ar[12])||!(numb%ar[13])||!(numb%ar[14])||!(numb%ar[15])||!(numb%ar[16])||!(numb%ar[17])||!(numb%ar[18])||!(numb%ar[19])||!(numb%ar[20])||!(numb%ar[21])||!(numb%ar[22])||!(numb%ar[23])||!(numb%ar[24])||!(numb%ar[25])||!(numb%ar[26])||!(numb%ar[27])||!(numb%ar[28])||!(numb%ar[29])||!(numb%ar[30])||!(numb%ar[31])||!(numb%ar[32])||!(numb%ar[33])||!(numb%ar[34])||!(numb%ar[35])||!(numb%ar[36])||!(numb%ar[37])||!(numb%ar[38])||!(numb%ar[39])||!(numb%ar[40])||!(numb%ar[41])||!(numb%ar[42])||!(numb%ar[43])||!(numb%ar[44])||!(numb%ar[45])||!(numb%ar[46])||!(numb%ar[47])||!(numb%ar[48])||!(numb%ar[49])||!(numb%ar[50])||!(numb%ar[51])||!(numb%ar[52])||!(numb%ar[53])||!(numb%ar[54])||!(numb%ar[55])||!(numb%ar[56])||!(numb%ar[57])||!(numb%ar[58])||!(numb%ar[59])||!(numb%ar[60])||!(numb%ar[61])||!(numb%ar[62])||!(numb%ar[63])||!(numb%ar[64])||!(numb%ar[65])||!(numb%ar[66])||!(numb%ar[67])||!(numb%ar[68])||!(numb%ar[69])||!(numb%ar[70])||!(numb%ar[71])||!(numb%ar[72])||!(numb%ar[73])||!(numb%ar[74])||!(numb%ar[75])||!(numb%ar[76])||!(numb%ar[77])||!(numb%ar[78])||!(numb%ar[79])||!(numb%ar[80])||!(numb%ar[81])||!(numb%ar[82])||!(numb%ar[83])||!(numb%ar[84])||!(numb%ar[85])||!(numb%ar[86])||!(numb%ar[87])||!(numb%ar[88])||!(numb%ar[89])||!(numb%ar[90])||!(numb%ar[91])||!(numb%ar[92])||!(numb%ar[93])||!(numb%ar[94])||!(numb%ar[95])||!(numb%ar[96])||!(numb%ar[97])||!(numb%ar[98])||!(numb%ar[99]))
                  {
                  ++numb;
                  }


with this:
1
2
3
4
5
6
7
8
9
int primecount, running_result;

running_result = 0;
while(!running_result) {
	for(primecount = 0; primecount <= 99; primecount++){
		running_result = running_result || (numb % ar[primecount]);
	}
	if(running_result) numb++;
}


i need two loops to do what you do in one! one loop for your long list of mod checks and a second loop for incrementing your numb variable.
i have no idea what you are doing though. i'm just converting code.

so, any integer that doesn't contain any smaller prime numbers is a prime number? neat. i had no idea.

some guy told me
A prime number is a positive integer that has exactly two positive integer factors, 1 and itself.
so i probably would've checked every number inbetween, or at least as far as the square root.

something like this:
1
2
3
4
5
6
int isnumberaprime(long long number, long long lessthannumber)
{
	if(lessthannumber < 2) return(0); // must be prime. done.
	if((number % lessthannumber) == 0) return(1); // not prime. done.
	else return(isnumberaprime(number, lessthannumber - 1)); // maybe prime. keep going.
}

but your method has far fewer comparisons and is much faster!


by the way, does long long really do anything or is that to impress the ladies? i've only seen guys with unsigned longs.
Some compilers make long long twice as long as long, others just throw a compiler error.
Topic archived. No new replies allowed.