# [SOLVED] Finding Prime numbers between l and u.

• (2 Pages)
• 1
• 2

## 16 Replies - 1960 Views - Last Post: 28 August 2013 - 05:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=327814&amp;s=b56c5a43908dbd3751e0cc9e4df786c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

# [SOLVED] Finding Prime numbers between l and u.

Posted 27 August 2013 - 06:55 PM

I am brand spankin' new to C but am familiar with Java, I'm trying to write a program that finds all the prime numbers between the two numbers that a user enters, here is my code:

```#include<stdio.h>
#include<math.h>

int main()
{
int l, u, i;

printf("Enter the two numbers l and u:\n");
scanf("%d %d", &l, &u);

for(i = l; i <= u; i++)
{
isPrime(i, u);
}

return 0;
}

int isPrime(int num, int u)
{
long root = sqrt(u);
int i;

for(i = 2; i <= root; i++)
{
if(num % i == 0)
break;
}
printf(num + " ");
}
```

Here are the results:

This post has been edited by thorax232: 28 August 2013 - 11:37 AM

Is This A Good Question/Topic? 0

## Replies To: [SOLVED] Finding Prime numbers between l and u.

### #2 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 27 August 2013 - 08:02 PM

Hi, with printf the first parameter is a format string which can format the remaining parameters.

```29	    printf(num + " ");

```

should be like
```29	    printf("%d ", num);

```

printf

### #3 temp_0001

Reputation: 1
• Posts: 31
• Joined: 25-August 13

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 27 August 2013 - 08:19 PM

```int isPrime(int lo, int hi)
{
long root = (long) sqrt(hi);
int i;

for(i=2; i < root; i++)
{
if(lo % i == 0)
return 0;
}

return 1;
}

```

```//before your for loop
int r;
printf("Prime Numbers between %d and %d\n", l, u);
r = isPrime(i, u);
if(r == 1)
printf("\t%d\n", i);

```

### #4 temp_0001

Reputation: 1
• Posts: 31
• Joined: 25-August 13

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 27 August 2013 - 10:20 PM

how was my post not helpful?

### #5 Skydiver

• Code herder

Reputation: 6064
• Posts: 20,878
• Joined: 05-May 12

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 27 August 2013 - 10:59 PM

Your poorly named variable lo makes things more confusing rather than clearer.

And to make matters worse, your code now incorrectly identifies the number 9 as a prime number if the user enters 8 for L, and 10 for U.

### #6 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 07:54 AM

temp_0001, on 28 August 2013 - 01:20 AM, said:

how was my post not helpful?

You should also read the rules about just posting code to answer people's questions. Especially without any comments or explanation to go with it.

### #7 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 09:30 AM

Well it did help, "lo" problems aside, I just named that num. Also I found that when you use the square root with a variable sqrt(variable), you have to compile with -lm so it was like gcc -lm prime.c.

Anyways, thanks temp_0001, I'm up and running now!

This post has been edited by thorax232: 28 August 2013 - 09:31 AM

### #8 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 09:41 AM

thorax232, on 28 August 2013 - 12:30 PM, said:

Well it did help, "lo" problems aside, I just named that num. Also I found that when you use the square root with a variable sqrt(variable), you have to compile with -lm so it was like gcc -lm prime.c.

Anyways, thanks temp_0001, I'm up and running now!

*EDIT*:
If you didn't read it, then here's the relevant part:

Quote

And to make matters worse, your code now incorrectly identifies the number 9 as a prime number if the user enters 8 for L, and 10 for U.

This post has been edited by vividexstance: 28 August 2013 - 09:41 AM

### #9 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 11:32 AM

And that post wasn't helpful because?

### #10 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 11:32 AM

vividexstance, on 28 August 2013 - 09:41 AM, said:

This isn't happening in my code, I thank you for your concern but watch the language you use. This comes across as condescending.

Here's my final code, if you want to run it and check it I would appreciate that, it's up to you.
```#include<stdio.h>
#include<math.h>

int isPrime(int, int);

int main()
{
int l, u, i, count;

printf("Enter the two numbers l and u:\n");
scanf("%d %d", &l, &u);

int r;
count = 0;
for(i = l; i <= u; i++)
{
r = isPrime(i, u);
if(r == 1)
{
printf("%4d", i);
count++;
}

if(count == 10)
{
printf("\n");
count = 0;
}
}

printf("\n");
return 0;
}

int isPrime(int num, int hi)
{
long root = sqrt(hi);
int i;

for(i = 2; i <= root; i++)
{
if(num % i == 0)
return 0;
}
return 1;
}
```

### #11 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 11:34 AM

thorax232, on 28 August 2013 - 02:32 PM, said:

vividexstance, on 28 August 2013 - 09:41 AM, said:

This isn't happening in my code, I thank you for your concern but watch the language you use. This comes across as condescending.

That was a simple question, based on the other post you made it seemed like you just took temp_0001's code, I was just making sure you saw what Skydiver had posted.

### #12 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 11:36 AM

vividexstance, on 28 August 2013 - 11:34 AM, said:

That was a simple question, based on the other post you made it seemed like you just took temp_0001's code, I was just making sure you saw what Skydiver had posted.

Yes I can read and it'd be nice if you leave that up to me. Also not playing the -1 game because you don't like something isn't funny. Conversation over. Thanks for the help.

### #13 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 11:40 AM

thorax232, on 28 August 2013 - 02:36 PM, said:

vividexstance, on 28 August 2013 - 11:34 AM, said:

That was a simple question, based on the other post you made it seemed like you just took temp_0001's code, I was just making sure you saw what Skydiver had posted.

Yes I can read and it'd be nice if you leave that up to me. Also not playing the -1 game because you don't like something isn't funny. Conversation over. Thanks for the help.

I don't like people abusing the rep system. Especially people who have been here as long as you.

### #14 temp_0001

Reputation: 1
• Posts: 31
• Joined: 25-August 13

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 01:49 PM

I'm sorry I posted the code the way I did. I usually don't comment obvious statements, but I forgot that I was typing for a newbie. My code has an off-by-one error in the loop. "<" should be "<=".

I forgot to tell you that it needs to be linked with "-lm", but you got it.

### #15 temp_0001

Reputation: 1
• Posts: 31
• Joined: 25-August 13

## Re: [SOLVED] Finding Prime numbers between l and u.

Posted 28 August 2013 - 02:24 PM

there is somethibg wrong with the for loop. numbers shouldn't be modulused by themselves as that would return a wrong answer, so the following code will prevent the number from modulusing to itself.

```for(i  = 2; i <= root && i < num; i++)

```

the && (and) will make sure that the "i <= root" and "i < num" are true before the loop is entered. i used 1 and 100 as my input for the original code. then i used printf calls (calls are when a program uses a function) to debug the code to see what is going on.

```printf("\n[debug]\n");
for(i = 2; i <= root; i++)
{
printf("\t[%d] % [%d] == [%d]\n", num, i, num % i);
// rest of code
// \t is a c escape chracter that means it will display a tab

```

i believe the fix should solve the problem mentioned.