ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Originally posted by Mercurius You are right, I was wrong 15 can be divided by 3. How do you suggest to compute if one number is prime or not?
Got it, thanks alot for the help. One last question about flowcontrol. Cant I do in C something like go to line 15?
Yes, but that kind of construct should be avoided. This works:
Code:
int gcd(int a, int b)
{
int q, r;
q = (a > b) ? a : b;
r = (a < b) ? a : b;
a = q;
b = r;
q = a % b;
r = a - q *a;
return (r == 0) ? q : gcd(b, r);
}
int euler(int n)
{
int a = 0;
int i = 1;
loop:
a = a + (gcd(a, i) == 1) ? 1 : 0;
i = i + 1;
if (i > n) return a;
goto loop;
}
but this is better
Code:
int gcd(int a, int b)
{
int q, r;
q = (a > b) ? a : b;
r = (a < b) ? a : b;
a = q;
b = r;
q = a % b;
r = a - q *a;
return (r == 0) ? q : gcd(b, r);
}
int euler(int n)
{
int a = 0;
for (int i = 1; i <= n; ++i) {
a += (gcd(a, i) == 1) ? 1 : 0;
}
return a;
}
Caution: The above code is C++, not C, because of the declaration of i inside the for.
(FYI: the Euler function return the number of numbers less than "n" that are relativel prime to n. And the "gcd" function returns the "greatest common divisor" of two numbers. If the value is 1, the two numbers are "relatively prime." Notice that the "gcd" function is recursive, so it will fail for "large" numbers. [The code was for an algebra homework problem I was working on a few weeks ago.])
Last edited by PTrenholme; 10-19-2005 at 05:08 PM.
Well, all say not to use goto. Sure, it may look ugly, but come one, what would you prefer? Art or quick functionality? Or both? Why should anyone discard a function just beacause it's ugly??
it's not only the ugliness in the problem. If you put 2 gotos in the code and you jump 40-50 lines up or down the program becomes hard to read and follow. Especially people who now learn C should not use it because they don't know where its OK to do this and where not. You can do everything without the goto.
Testing if a number is prime or not isn't hard (It can be done in polynomial time). The hard part is to calculate the prime descomposition.
And, in any case, the problem isn't unsolvable. It is just "hard" in the sense that it takes a lot of time for the computer to be done. Here is a pseudo-algorithm for primality testing: If N is the number to be tested,
for i from 2 to sqrt(N): if i divides N then N isn't a prime number.
You can use the same idea to calculate the descomposition. The problem is that it takes quite long time (or at least almost every computer scientist thinks so).
Originally posted by enemorales Just a clarification:
Testing if a number is prime or not isn't hard (It can be done in polynomial time). The hard part is to calculate the prime descomposition.
And, in any case, the problem isn't unsolvable. It is just "hard" in the sense that it takes a lot of time for the computer to be done. Here is a pseudo-algorithm for primality testing: If N is the number to be tested,
for i from 2 to sqrt(N): if i divides N then N isn't a prime number.
You can use the same idea to calculate the descomposition. The problem is that it takes quite long time (or at least almost every computer scientist thinks so).
I haven't stated it clear. What I meant is that there is no mathematical equation to check if a number is prime. And this is proven.
Last edited by perfect_circle; 10-20-2005 at 07:33 AM.
Originally posted by perfect_circle
I haven't stated it clear. What I meant is that there is no mathematical equation to check if a number is prime. And this is proven.
Actually, that depends on your definition of "mathematical equation." As you stated, the problem is solvable for any integer, and you even gave a procedure to follow to solve the problem.
I'd call that an "equation" in the general sense that it defines a map from the set of integers to the set of prime numbers.
FWIW - the fastest possible way to determine primaility is to create a static array with of all the prime numbers up to the limit of your variable's datatype. In other words, you calculate them once, then store them in code.
Then use bsearch on the array.
You can download a list of primes here if you don't want to calculate them:
It also has a simple C program to create those prime numbers, but running that code every time your program starts up is a bad idea in terms of performance. So keeping the primes in memory, though memory expensive (not good from ARM development), is the fastest possible implementation.
If you know the numbers input will always be small (say less than 8192) then use a seive and store the data in an array.
Here is an old version, I think it will compile on most machines:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.