Method for generating prime numbers
C++ code:
using namespace std;
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
print_prime(i);
}
return 0;
}
This is assuming print_prime(i) runs in O(1) time.
Guys, please don't steal my work.
How's that going to compile? The function print_prime(int) isn't defined in iostream. In other words, where's the algorithm?
There is a compiler optimization called tail recursion that would generate the code for the iterative version given the recursive one. I know gcc supports it, although I don't know about other C compilers.
You have to set up your recursion to pass all the information the next iteration needs as arguments, but IIRC you actually did that the first time around.
_________________
"A dead thing can go with the stream, but only a living thing can go against it." --G. K. Chesterton
Geez, tough crowd.
I guess it's always going to be awkward to make such a joke in a community well-known for being bad at understanding subtext... Is it being a jerk to make a non-obvious joke here, knowing that a lot of people won't get it? Or does it help to "train" people to spot jokes? This is something that I have been wondering ever since that "Marxist professor" thread.
I don't mean to be rude, but are you sure that you're not becoming a crank?
http://en.wikipedia.org/wiki/Crank_(person)
The idea that you have found a method for generating primes that is better than any method that has ever been found, given that you have not trained as a mathematician, is bordering on "impossible". In fact, I would go even further and say that it actually is impossible. So much work has been put into this problem that any improvement would take at least fifty pages to write down. If your method is written on a couple of pages... there's something wrong with it. There just is.
Comments that you have made seem to indicate that you are no closer to generating primes than you ever were. You have a method that generates numbers that are "either primes or products of primes". That includes every single natural number greater than 1. It's not an achievement at all.
You may be right, but I don't think so. I'll work on it again and get back to you.
There is a compiler optimization called tail recursion that would generate the code for the iterative version given the recursive one. I know gcc supports it, although I don't know about other C compilers.
You have to set up your recursion to pass all the information the next iteration needs as arguments, but IIRC you actually did that the first time around.
Well, we're assuming an optimizing compiler, and I know at least the older Microsoft Visual C++ compilers in their "Standard Edition" or "Learning Edition" (perhaps now "Express Edition") did not include an optimizing C or C++ compiler.
Thanks. I mean, the NSA says thanks. Of course.
Reminds me of my master code that produces all positive integers in O(n) time.
I definitely want to hear how it goes. :3
I guess it's always going to be awkward to make such a joke in a community well-known for being bad at understanding subtext... Is it being a jerk to make a non-obvious joke here, knowing that a lot of people won't get it? Or does it help to "train" people to spot jokes? This is something that I have been wondering ever since that "Marxist professor" thread.
If you want a shoulder pat, try post it on stack overflow. ;3
Since you're not telling us anything about this magical algorithm, we're entirely unable to assess it. But I do have an interesting question. You've already said that it returns both primes and multiples of primes, and these need weeding out - the big question I have is whether it returns ALL the primes in a given range? Or just some of them?
I mean, I think 7420738134811 is a prime number. If it isn't, it's a multiple of one. But I couldn't tell you whether there were any more in the vicinity.
(Actually, aren't all numbers multiples of primes?)
Since you're not telling us anything about this magical algorithm, we're entirely unable to assess it. But I do have an interesting question. You've already said that it returns both primes and multiples of primes, and these need weeding out - the big question I have is whether it returns ALL the primes in a given range? Or just some of them?
I mean, I think 7420738134811 is a prime number. If it isn't, it's a multiple of one. But I couldn't tell you whether there were any more in the vicinity.
(Actually, aren't all numbers multiples of primes?)
It generates only primes larger than 7, and it appears to generate all such primes. It also appears I was prematurely optimistic about its limited production of products of primes (not multiples of primes). It is producing more than I expected.
I don't mean to be rude, but are you sure that you're not becoming a crank?
http://en.wikipedia.org/wiki/Crank_(person)
The idea that you have found a method for generating primes that is better than any method that has ever been found, given that you have not trained as a mathematician, is bordering on "impossible". In fact, I would go even further and say that it actually is impossible. So much work has been put into this problem that any improvement would take at least fifty pages to write down. If your method is written on a couple of pages... there's something wrong with it. There just is.
Comments that you have made seem to indicate that you are no closer to generating primes than you ever were. You have a method that generates numbers that are "either primes or products of primes". That includes every single natural number greater than 1. It's not an achievement at all.
You may be right, but I don't think so. I'll work on it again and get back to you.
Added 2:18 [oops. I thought I was editing]
I appreciate your skepticism. When I discovered it I was just playing with primes, discovered a set of properties, and imported a little trick I had invented from arithmetic to maintain that set of properties while changing the number. It is really that simple.
I didn't think I had discovered anything great. I thought surely someone else had found the same thing. Same mistake I made with my stolen paper on the Fibonacci sequence. Same method of discovery, as well. "Life is frittered away with detail. Simplify. Simplify." Thoreau
I am not your usual analyst. My methods always seek to simplify the problem, not find a more complex way to solve it. Being an Aspie, I tend to notice patterns, then I exploit them.
I don't know how to write a program to run it, but I think that would be rather simple.
All numbers are primes, sums of primes, or products of primes. My method generates primes greater than 7, and only products of primes larger than 5. More than I anticipated, but fewer than all.
More later.
Last edited by jackmt on 18 Feb 2012, 10:26 pm, edited 2 times in total.
F(1) = 2
F(2) = 3
F(3) = 5
and so on.
ruveyn
F(n) = if (n=1) then 2 else X(F(n-1)+1)
X(n) = if (P(n) = 1) then n else X(n+1)
P(n) = if (D(n-1, n) = 0) then 1 else 0
D(i, n) = if (i <= 1) then 0 else (if (i | n) then 1 else D(i-1, n))
D(i, n) returns 1 when some number greater than 1 and less than or equal to i divides n.
P(n) returns 1 if n is prime.
X(n) returns the smallest prime equal to or greater than n.
F(n) returns the nth prime number.
Not fast at all, but computable.
_________________
"A dead thing can go with the stream, but only a living thing can go against it." --G. K. Chesterton
An interesting snippet - apart from 2 and 3, all prime numbers can be expressed as 6n+1 or 6n-1.
A little algebra, however, soon explains why.
6n is not prime (divides by 6!)
6n+2 and 6n+4 (or 6n-2) divide by 2.
6n+3 (or 6n-3) divides by 3.
So any prime necessarily much be one of the two remaining options - 6n+1, or 6n+5 (6n-1).
Umm, I would like to see this algorithm explicitly, I would be very fasincated to see it actually.
In my highschool years, I found a book that my dad had that listed prime numbers up to about 10,000 or so, and I became very fascinated with it, trying to find the pattern in the whole. I vehemently disagree with this common conception that is random. I always felt like there must be some underlying chatoic pattern to the primes. I mean, how could be there not be?
I do think an Indian mathematician came up with a pretty good method. I will look up and report on it afterward.
...
So any prime necessarily much be one of the two remaining options - 6n+1, or 6n+5 (6n-1).
...
To be a bit more pedantic... (
Apart from nothing, all prime numbers can be expressed as n.
Apart from 2, all prime numbers can be expressed as 2n+1.
Apart from 2 and 3, all prime numbers can be expressed as 6n+1 or 6n+5.
...
This approach generalizes: http://en.wikipedia.org/wiki/Primorial
I'd guess that, if working on numbers with a view to establishing their primality, storing values in base 30030 (2*3*5*7*11*13) would give a quick test that eliminated most composites.
_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer
