I finally did my homework but..

Write your question here.
I made a function for c++ and my professor said I cannot use sqrt(n) in my function. I have no clue how to make this function without using sqrt(n).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool isprime(int n)
{
	int i;
	int j;
	int k;
	int f=0;
	int d=sqrt(n);
	for(i=2;i<=d;i++)
	{
		if(n%i==0)
		{
			f=1;
			i=i+d;
		}
	}
		if(f==1)
		return false;
		else 
		return true;

}
What about increasing an integer say, i by means of a loop from 1 to n, and if n%i==0 occurs only twice, then the number is prime?

Aceix.
Last edited on
@Aceix
Sorry but I didn't really understand what you said can You write it with the code?
how about n/2? :) It would simply mean it does some unnecessary computation... it's less efficient, but you will not be using sqrt.
What's the reason he's not allowing you to use sqrt?
On the other hand one thing that pops into my mind now is maybe your professor is asking you to implement the sieve of Eratosthenes?
Last edited on
I meant something like:
1
2
3
4
5
6
int num_of_factors=0;
for(int i=1;i<n;i++)
{
     if(n%i==0) num_of_factors++;
}
if(num_of_factors==2) cout<<"Prime number!!";


where n is the number to be tested.
and of course this style is resource consuming and may waste time for large numbers.

Aceix.
@Hiten
n/2 is not equal to square root of n isn't it?

I have no clue she has 1.2 point overall in ratemyprofessor.com that might explain your second question

what is the sieve of Eratosthenes??

@Aceix
I'm so dumb and newby.... I cannot really understand your code
well now there is one more bigger problem that I faced
I just contacted the professor, she said I cannot use for. Now, her class doesn't really make sense for me. she want me to use while, but for is shorter than while isn't it?
1
2
3
4
5
6
7
8
for(i=2;i<=d;i++)
	{
		if(n%i==0)
		{
			f=1;
			i=i+d;
		}

1
2
3
4
5
6
7
8
9
int i = 1;
while (i <=d){
    i = i + 1
    {
        if(n%i==0)
		{
			f=1;
			i=i+d;
		}

so now i'm working with change all for loop to while
Like I said in the other thread this is why you should write your own code and not use your friends code...She probably only wants you to use what you have learned in class so far. As far as the sieve of eratosthenes I told you what it was the other day :P http://www.cplusplus.com/forum/beginner/125481/#msg680556

Basically it looks something like(not optimized):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int n = 100; //all primes less than 1000
const int sqrt = 10;
std::vector<bool> primes(n,true);
primes[0] = false;
primes[1] = false;

for(int i = 2; i < sqrt; ++i) //this could be optimized
{
    if(primes[i])
    {
        for(int j =i*i; j < n; j += i)
        {
            primes[j] = false;
        }
    }
}

for(int i = 2; i < n; ++i)
{
    if(primes[i]) std::cout << i << " is prime" << std::endl;
}



Also for loops increment at the end so the equiv while loop would be:

1
2
3
4
5
6
7
8
9
10
int i = 2;
while(i <= d)
{
    if(n%i==0)
    {
        f = 1;
        i += d;
    }
    ++i;
}


Anyways I think they want you to do something more like:

1
2
3
4
5
6
7
8
bool prime = true;
int i = 2;

while(prime && i <= d)
{
    if(i%d==0) prime = false;
    ++i;
}

Last edited on
@giblit
I wanted to do it by myself if I know how to do it.
All of my classmates don't know how to do it she basically teach 2 + 2 and want us to do 2 * 5 that's why I failed to do it by myself. I was doing it for 5 hours and I asked the professor several times and she doesn't help me and keep saying "you cannot use this, think" she kept said "think" and never gave me a single help. I am ashamed to get my friend to do it for me. I really want to do it by myself and that's why I joined cplusplus But thank you for your advise
Topic archived. No new replies allowed.