Velocity Reviews > Prime numbers

# Prime numbers

Don
Guest
Posts: n/a

 08-31-2005
> How do you write a program to print the prime numbers
> up to 1 million?

#include <stdio.h>

/* This program implements a blindingly fast O(n^n) algorithm
to find prime numbers, using an elegant recursive method. */
int p(int n, int m, int d)
{
int i, r = m != n;
for(i=0; d && (i<n); i++)
r *= p(n,i*m,d-1)|!p(i,1,i);
return r;
}

/*------------------------------------------
Print primes up to the requested value
--------------------------------------------*/
int main(int argc, char* argv[])
{
int n;
for(n = 2; n<1000000; n++)
printf("%d is%s prime\n",n, p(n,1,n)?"" : " not");
return 0;
}

elzacho
Guest
Posts: n/a

 08-31-2005
Yeah, that is fast. Any day now I should know if 6 is prime or not...

James Dow Allen
Guest
Posts: n/a

 09-04-2005

#if 0
Don wrote:
> > How do you write a program to print the prime numbers
> > up to 1 million?

>

The following program, believe it or not, prints every prime
(in "Stone-age" or unary code) in order! (subject, like any
program, to precision and overflow issues on your machine).
This version is not user-friendly: it may core-dump when
the primes get too big for it to handle.

It is based on an invention by John H. Conway, as reported
in _Ancient Puzzles_ by Dominic Olivastro.

I''m using this primality program for the Zimmermann Contest.
#endif

#include <stdio.h>

double Prog[] = {
/* This sequence of 14 fractions *is* the prime
* generation program !! The function below is just
* a (very crude) "Minsky-machine" interpreter.
*/
17/91., 78/85., 19/51., 23/38., 29/33., 77/29., 95/23.,
77/19., 1/17., 11/13., 13/11., 15/14., 15/ 2., 55/ 1.,
};

int ix, oic = 2, nic;
double f;

main()
{
for (nic = ix = 0; !nic; ix++) {
f = Prog[ix] * oic + .01;
nic = (((int)f != (int)(f - .02)) * (int)f);
}
oic = nic--;
if (!(nic + 1 & nic)) {
do fprintf(stderr, "1");
while (nic >>= 1);
fprintf(stderr, "\n");
}
main();
}

/* Best regards, James */

Don
Guest
Posts: n/a

 09-06-2005
As I said, it is blindingly fast.

As in "You'll go blind waiting for it to decide if 8 is prime."

Kenneth Brody
Guest
Posts: n/a

 09-06-2005
James Dow Allen wrote:
[...]
> The following program, believe it or not, prints every prime
> (in "Stone-age" or unary code) in order! (subject, like any
> program, to precision and overflow issues on your machine).
> This version is not user-friendly: it may core-dump when
> the primes get too big for it to handle.

[...snip...]

Apparently, only 2, 3, and 5 ("11", "111", and "11111") are prime, as
the program exits at that point. (Either that, or 7/"1111111" is "too
big for it to handle".)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

Flash Gordon
Guest
Posts: n/a

 09-06-2005
Don wrote:
> As I said, it is blindingly fast.

What is?

> As in "You'll go blind waiting for it to decide if 8 is prime."

Seeing as you provide no context I have no idea what you are talking
about. Search the group for instructions on how to get context in Google.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

Richard Harter
Guest
Posts: n/a

 09-07-2005
On Tue, 06 Sep 2005 19:31:56 +0100, Flash Gordon
<(E-Mail Removed)> wrote:

>Don wrote:
>> As I said, it is blindingly fast.

>
>What is?
>
>> As in "You'll go blind waiting for it to decide if 8 is prime."

>
>Seeing as you provide no context I have no idea what you are talking
>about. Search the group for instructions on how to get context in Google.

If you don't know what he is talking about then you have nothing to
contribute to the conversation. Try practicing not contributing.

Richard Harter, http://www.velocityreviews.com/forums/(E-Mail Removed)
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.

Alan Balmer
Guest
Posts: n/a

 09-07-2005
On Wed, 07 Sep 2005 15:52:37 GMT, (E-Mail Removed) (Richard Harter) wrote:

>On Tue, 06 Sep 2005 19:31:56 +0100, Flash Gordon
><(E-Mail Removed)> wrote:
>
>>Don wrote:
>>> As I said, it is blindingly fast.

>>
>>What is?
>>
>>> As in "You'll go blind waiting for it to decide if 8 is prime."

>>
>>Seeing as you provide no context I have no idea what you are talking
>>about. Search the group for instructions on how to get context in Google.

>
>If you don't know what he is talking about then you have nothing to
>contribute to the conversation. Try practicing not contributing.
>

Advising a would-be contributor to provide proper attribution *is* a
contribution.
--
Al Balmer
Balmer Consulting
(E-Mail Removed)

Randy Howard
Guest
Posts: n/a

 09-07-2005
Don wrote
(in article
<(E-Mail Removed). com>):

> As I said, it is blindingly fast.

What is?

Step 1: Learn how to use newsgroups properly.

Step 2: Learn what newsgroups have the topics you are interested
in.

Step 3: Post there, with appropriate context included, so others
don't have to go searching for the post you are referencing.

--
Randy Howard (2reply remove FOOBAR)

Flash Gordon
Guest
Posts: n/a

 09-07-2005
Richard Harter wrote:
> On Tue, 06 Sep 2005 19:31:56 +0100, Flash Gordon
> <(E-Mail Removed)> wrote:

<snip a reply with no context>

>>Seeing as you provide no context I have no idea what you are talking
>>about. Search the group for instructions on how to get context in Google.

>
> If you don't know what he is talking about then you have nothing to
> contribute to the conversation.

Incorrect as evidenced by the fact that I *did* contribute. I suspect
that in this instance most of the regulars here would consider my
contribution more valuable than yours.

> Try practicing not contributing.

I have plenty of practice at that. People who have read this group to
any real extent will have seen lots of examples of me not contributing.
However, in this case Don obviously had not yet leant about providing
context and I decided that I was as good a person as any to point out
the error of his ways.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.