Functions

I am trying to write a program that will output the prime numbers between two numbers 1 and 2 using a function. If anyone can offer some pseudo code or code for refrencing. I would like to learn it rather then copy.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <fstream>

using namespace std;

bool isPrime(double Number){

for (int i = 2; i*i <= Number; i++){
    if (num%i==0) return false;
    
}
    
    return true;

}

int main(){
    
    
    return 0;
}


This is what I have so far. Could I make this work and any pointers?
Should work except you haven't called the function from main.
Also read about user input if you dont it yet and have user input the number.
closed account (2AoiNwbp)
num == Number? it is not declared, so it won't compile

You can also solve it with recursion. It would be a nice challenge to understand how it works, because of the branches. Also to understand recursion, in case you don't know already.
It is a function that grows up quickly, so you can also save the values found in a look up table to store previous values and avoid branching.

regards,
Alejandro
Last edited on
closed account (2AoiNwbp)
I would like to learn it rather then copy.

That's good. You learn by both, I mean by writing programs as above, and understanding how to solve problems using different algorithms. That's why I told you about recursion. You won't learn about primes, you already know what they are...
If you can solve it by recursion, you can do it with a for loop. Although with a for loop is easier, you can get more knowledge by solving it by recursion.

I found a link which I think will help you solving this by recursion, although it hasn't got the best algorithm, and you'll still need to add a lookup table. Still, need to learn what it is, in case you don't know already.

http://www.danzig.us/cpp/recursion.html

regards,
Alejandro
Last edited on
Wow, Thanks for the information. I am not the "smartest tool in the shed". I took to dreamweaver, databases, and gamemaker easier then C++. Which has made me want to learn C++. I started on another code as an experement after my research on ways to do this.

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
 

#include<iostream>
#include<iomanip>
#include<conio.h>

using namespace std;

int main() {

clrscr();

int Num, i, Counter, Number;

cout << "Enter any number for the prime numbers between 2 and your number. \n";
cin >> Number;

for (Number=2; Number<=Num;Number++){

    Counter = 0;

    for(i=2;i<=Number/2;i++){

        if(Number%i==0){

            Counter++;
            break;

        }
   }

   if(Counter==0 && Number!=1)

    cout << Number << setw(3);

}

getche();
return 0;
}


This looked like the easier option, shame on me! But getche and clrscrn are running into errors. I have not built any of the code yet so don't laugh at errors!

I gave my program access to the #include<conio.h> library.

aabuezo, thank you I will read through the information in your link and come back.
closed account (2AoiNwbp)
I am not the "smartest tool in the shed"

Neither I am. We learn from each other. ;)

conio.h is a library of the Borland compiler, you are probably using another one.
You can just avoid clrscr(), you don't need to clear the screen, and you can replace getche() with system("pause"); in this case.
Nevertheless, what is the limit of the outer loop? num is never initialized.

By the way, this is called brute force (as in your algorithm), cause you search the whole search space. That is, you search all the numbers from 1 to Number/2 (the result must be there). It is a simple way to solve a numeric problem, whenever you search in the correct search space (the result exists in there).

regards,
Alejandro
Last edited on
so recursion is a function that can call itself untill it returns a value? sounds like fun. I will give it a go.
closed account (2AoiNwbp)
Yes. But remember, you need to solve simpler versions of the same problem till you reach what is called the base case (1 and 2 in this case).

Each call of the function will push what is called a stack frame into memory, filling it with replications of the function calls. When the base case is reached, it starts to pop the stack frames, freeing the memory.

If you never reach the base case, program will run out of memory. This has to do with branching. When you find the algorithm, analyze the stack frames (function calls) to understand branching. Then, you`ll need a look up table to avoid it.

regards,
Alejandro
Thanks Alejandro,

I understand it, but can't wrap my head around it. If that makes sense?

Do you think you could help with some pseudo code fo referencing when it comes to this program. Detailed as you will.

That would help me ALOT. If not, no problem I will keep trying.
Last edited on
closed account (2AoiNwbp)
I made this code for you to understand. First, you need to understand how it works, then you try to find the way of saving previous values to avoid branching.

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
#include <iostream>

using std::cout;
using std::endl;

bool IsPrime(int n, int i);

int main()
{
	int i = 1, num = 10; 
	while(i <= num) {
		cout << "Testing: " << i << endl;
		cout << "Calling IsPrime(" << i << ")" << endl;
		if(IsPrime(i, i/2))		
			cout << i << " is a prime number" << endl;
		i++;
	}
	system("pause");
	return 0;
}

bool IsPrime(int n, int i)
{
	cout << "i = " << i << endl;
	if(i == 0)	// Just to avoid dividing by zero
		return false;
	if(i == 1)	// Base case
		return true;
	else if(n%i == 0) {	// also a division, tests if n is divisible
			cout << n << " is divisible by " 
				 << i << " Not a prime" << endl;
			return false;
	}
	else		// if not, solve a simpler version of the same problem
			return IsPrime(n, i-1);
}


In the call to IsPrime(i, i/2), what the i/2 argument does, is both, first to check whether i is an even number in the first try, thus, it returns false without recursion, and second, sets the upper limit of the search space.
Then, you can increase num in main, lets say to 20, and add the line
 
cout << "*** New Stack Frame ***" << endl;

at the beginning of IsPrime, to see how the stack frames increase as the number increase, that's why you need to track previous values.

regards,
Alejandro
Last edited on
Topic archived. No new replies allowed.