Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge for you...
#1
Here is a challenge for you..

Find a enclosed image for a function to calculate the nth prime.

Your task is to produce the first 100 primes!

Hint: The factorial gets really large fast!?

Erik


[Image: nthprime.png]
Reply
#2
I've looked at this on You Tube, sieving is way way way faster!

But of course, the point is there IS a formula for primes Not whether it's very practical or not.
b = b + ...
Reply
#3
(04-20-2023, 02:02 PM)bplus Wrote: I've looked at this on You Tube, sieving is way way way faster!

Also requires a lot of memory. Although my program doesn't indicate it very well.

https://qb64phoenix.com/forum/showthread.php?tid=1536

To begin with I actually used one of Freebasic's example programs which was Eratosthenes' sieve.

It was a bit complicated, but my implementation was an array of bits, which was still quite large to get to a target of 100 billion. This was although all even numbers after 2 were excluded, and the first member of the bit array represented 3, the next 5, the one after 7 and so on.
Reply
#4
The giant factorials don't scare me now that I have String math I can do them all day. It's the power stuff that hangs me up.
b = b + ...
Reply
#5
no chance to produce 100 primes with that lame formula unless you use bignum
here are the first 6 primes Rolleyes 
Code: (Select All)
Dim As Long i

For i = 1 To 6
    Print i, nprime(i)
Next

Function factorial# (n As Long)
    Dim As Long i
    Dim As Double f
    If n = 0 Or n = 1 Then
        factorial = 1
        Exit Function
    End If
    f = 1
    For i = 2 To n
        f = f * i
    Next
    factorial = f
End Function

Function nprime~&& (n As Long)
    Dim As Double s1, s2, s3, Pi: Pi = 3.1415926535897932384626433832795#
    Dim As Long i, j
    s1 = 0#
    For i = 1 To 2 ^ n
        s2 = 0#
        For j = 1 To i
            s3 = Cos(Pi * (factorial(j - 1) + 1) / j)
            s2 = s2 + Fix(s3 * s3)
        Next
        s1 = s1 + Fix(((n / s2) ^ (1 / n)))
    Next
    nprime = 1 + s1
End Function
Reply
#6
(04-20-2023, 05:07 PM)Jack Wrote: here are the first 6 primes Rolleyes 

Give us 94 more. Rolleyes



I could have sworn I wrote it down before: The book by PC Magazine author N.J. Rubenking about power-user programming in Turbo Pascal, which I had during the 1990's, contains an utility example of using the "string" type to compute arithmetic. Problem was that "string" type was able to hold only 255 characters. It was enough to hold 52! which the author wrote out on its wholesome in an earlier chapter of the book. I don't think he used any "in-house ASM" for it. He had written a separate "machine language" version of that utility though.

So bplus would still have to start the engine for the "string math" to get as high as 99!, saying that because I saw the (j-1)! somewhere in the originally-written formula.
Reply
#7
Code: (Select All)
n = 2

Do Until pcount = 100
    prv = 1
    For x = 2 To n - 1
        If n Mod x = 0 Then prv = 0: Exit For
    Next
    If prv Then Print n; " ";: pcount = pcount + 1
    n = n + 1

Loop


Reply
#8
Here is 500! curtesy of bplus string math
500! = 1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

1143 digits minus first 7 = 1136 (everything right of the space after = sign)
b = b + ...
Reply
#9
Are we now talking about prime numbers, or about the factorial?  Huh
Reply
#10
(04-20-2023, 07:33 PM)Kernelpanic Wrote: Are we now talking about prime numbers, or about the factorial?  Huh

The challenge was to compute the prime numbers involving factorial because it was in a formula that was presented in the first post. Petr presented code which doesn't conform but does produce the first 100 primes.

(04-20-2023, 06:34 PM)bplus Wrote: Here is 500! curtesy of bplus string math

Involve the "string math" into the formula presented in the first post, because that's the challenge. Smile
Reply




Users browsing this thread: 3 Guest(s)