LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2005, 05:02 PM   #16
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354

Quote:
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.
 
Old 10-19-2005, 07:11 PM   #17
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
Why should the "goto" be avoided? It sounds fairly straight and simple to use...
 
Old 10-19-2005, 08:03 PM   #18
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
http://crasseux.com/books/ctutorial/...bout-goto.html

http://www.le.ac.uk/cc/tutorials/c/ccccgoto.html

http://www.its.strath.ac.uk/courses/...00000000000000

http://www.samspublishing.com/articl...&seqNum=4&rl=1

those are the first 4 results in google when searching for "C programming goto"
 
Old 10-19-2005, 08:11 PM   #19
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
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??
 
Old 10-19-2005, 08:33 PM   #20
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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.

But It's your code.....
 
Old 10-20-2005, 03:53 AM   #21
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
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).
 
Old 10-20-2005, 07:31 AM   #22
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
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.
 
Old 10-20-2005, 08:59 AM   #23
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
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.
 
Old 10-20-2005, 09:31 AM   #24
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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:

http://www.totse.com/en/technology/s...y/prime10.html

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:

http://www.fpx.de/fp/Software/Sieve.html download sieve.c

Last edited by jim mcnamara; 10-20-2005 at 09:33 AM.
 
Old 10-20-2005, 09:54 AM   #25
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
/*
*
* Dumb program that generates prime numbers.
*/
#include <stdio.h>
#include <stdlib.h>

main(){
int this_number, divisor, not_prime;

this_number = 3;

while(this_number < 10000){
divisor = this_number / 2;
not_prime = 0;
while(divisor > 1){
if(this_number % divisor == 0){
not_prime = 1;
divisor = 0;
}
else
divisor = divisor-1;
}

if(not_prime == 0)
printf("%d is a prime number\n", this_number);
this_number = this_number + 1;
}
exit(EXIT_SUCCESS);
}

This is from the C book, it prints all prime number between 3 and 10.000
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Integer to Char gjagadish Programming 5 10-14-2005 10:09 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM
64 bit integer fahad153 Programming 9 08-26-2003 02:03 AM
length of integer variables daztheladd Programming 5 11-26-2002 07:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration