Extensible prime generator: Difference between revisions

Line 2,302:
[[File:Example.jpg]]
 
=== Using analytic formula for primes ===
{{Works with|GHC|8.10.7}}
{{Works with|primes|0.2.1.0}}
{{Works with|exact-real|0.12.5.1}}
 
There are closedanalytic formulasfunction for generating primes. One of such formula is the following:
: p(''n'') = [2''n'' (2''n''+1) {(2''n''-1)! ''C''}]
here [''x''] is the integral part of ''x'', {''x''} is the fractional part of ''x'', and ''C'' is a real constant (similar to [https://en.wikipedia.org/wiki/Mills%27_constant Mill's constant]). We can prove that there is a constant ''C'', such that p(''n'') is exactly the ''n''th prime number for any natural number ''n''. The first digits of ''c'' are ''c''=0.359344964622775339841352348439200241924659634...
Line 2,371:
(!) :: (RealFrac a, Num b) => a -> b
(!) = fromIntegral . snd . partial_sum (const 0) id 0 . round
 
-- Analytic function for n-th prime.
-- NB. Strictly speaking this function is not analytic, because it uses factorial, fractional part and round functions
-- To make it truly analytic you need to replace
-- fract x = acos (cos (2*pi*x)) / (2*pi)
-- round x = x - fract x
-- and use the Gamma function instead of factorial.
-- Then you will get analytic function prime :: CReal 0 -> CReal 0
 
prime n = round( 2*n*(2*n+1) * fract ( c * ((2*n-1)!)))</lang>