473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Prime numbers

Don
> 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;
}

Nov 15 '05 #1
13 2337
Yeah, that is fast. Any day now I should know if 6 is prime or not...
:)

Nov 15 '05 #2

#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 */

Nov 15 '05 #3
Don
As I said, it is blindingly fast.

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

Nov 15 '05 #4
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: <mailto:Th*************@gmail.com>
Nov 15 '05 #5
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.
Nov 15 '05 #6
On Tue, 06 Sep 2005 19:31:56 +0100, Flash Gordon
<sp**@flash-gordon.me.uk> 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, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Nov 15 '05 #7
On Wed, 07 Sep 2005 15:52:37 GMT, cr*@tiac.net (Richard Harter) wrote:
On Tue, 06 Sep 2005 19:31:56 +0100, Flash Gordon
<sp**@flash-gordon.me.uk> 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
re************************@att.net
Nov 15 '05 #8
Don wrote
(in article
<11*********************@z14g2000cwz.googlegroups. 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)

Nov 15 '05 #9
Richard Harter wrote:
On Tue, 06 Sep 2005 19:31:56 +0100, Flash Gordon
<sp**@flash-gordon.me.uk> 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.
Nov 15 '05 #10

Kenneth Brody wrote:
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".)


Well I *did* mention "overflow issues on your machine"!
(To save bandwidth I tend to be too parsimonious with the
smiley faces. Note that using gnu's 64-bit arithmetic won't
be enough to get up to 7.)

The (amazing) program, due to John Conway, is valid however
and would print primes with adequate hardware.

James

Nov 15 '05 #11
James Dow Allen <jd*********@yahoo.com> wrote:
(To save bandwidth I tend to be too parsimonious with the
smiley faces.)


I really don't believe that the handful (at most) bytes needed for a
clarifying emoticon are a particular hardship for anyone these days.
Unless you again neglected to include one...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #12
Oh, very good. A C code (translated of ForTran). Ok, very good. I'am
really like.
But...
Sometimes, we want to build ours own tools (poor e simply tools, is
true) and if that is her case... I advise you that begin like this:
* The only prime number that is "no-odd" is 2.
* Then you don't want verify if 4, 6, 8, 10, 12 is prime.
* A good algorhitm , in this case, probally is recursive.

Nice!

Nov 15 '05 #13
"Leonardo Andrade" <aa**********@gmail.com> writes:
Oh, very good. A C code (translated of ForTran). Ok, very good. I'am
really like.
But...
Sometimes, we want to build ours own tools (poor e simply tools, is
true) and if that is her case... I advise you that begin like this:
* The only prime number that is "no-odd" is 2.
* Then you don't want verify if 4, 6, 8, 10, 12 is prime.
* A good algorhitm , in this case, probally is recursive.


Look up "Sieve of Eratosthenes". It's about 2200 years old.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
0
by: ETM11871 | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
7
by: Caffiend | last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.