Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge for you...
#21
@bplus - Written out, it matches Julia. Now if I knew how to force the "...e+XXXX" I could check that. But I couldn't find it that quickly. But it is definitely somewhere in the 1565 pages of the manual.  Rolleyes
Reply
#22
(04-20-2023, 09:13 PM)Kernelpanic Wrote: @bplus - Written out, it matches Julia. Now if I knew how to force the "...e+XXXX" I could check that. But I couldn't find it that quickly. But it is definitely somewhere in the 1565 pages of the manual.  Rolleyes

Say, can Julia do the rest of the formula with the powers and COS that Eric posted at the start and Jack has in QB64 for 6 primes?
b = b + ...
Reply
#23
even if you could handle big factorials for n = 100 --> 2^100 --> 1267650600228229401496703205376
it would take forever to complete
Code: (Select All)
    For i = 1 To 1267650600228229401496703205376
        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
Reply
#24
@jack: Kudos on the first translation of the formula to BASIC.

Note: This program returns prime 7 as 129 so the formula may be incorrect. Huh 

Btw: Pi can be calculated as Atn(1)*4

Erik.
Reply
#25
@eoredson
the formula is correct, but 6 primes is the maximum that you are able to compute using double, with higher precision you could calculate more primes but it gets exponentially slow
Reply
#26
(04-21-2023, 12:50 AM)Jack Wrote: @eoredson
the formula is correct, but 6 primes is the maximum that you are able to compute using double, with higher precision you could calculate more primes but it gets exponentially slow

So, could you extend your program to _unsigned _integer64 for higher values? I tried to change Double to _Float with no result..
Reply
#27
I don't think so, because of the cos function what you need is more floating point precision, but using _Float is not going to help because the trig functions are evaluated to double precision but even if cos was evaluated to full _Float precision it would help very little
btw, did you watch the YouTube video? https://www.youtube.com/watch?v=j5s0h42GfvM
Reply
#28
(04-21-2023, 01:04 AM)eoredson Wrote:
(04-21-2023, 12:50 AM)Jack Wrote: @eoredson
the formula is correct, but 6 primes is the maximum that you are able to compute using double, with higher precision you could calculate more primes but it gets exponentially slow

So, could you extend your program to _unsigned _integer64 for higher values? I tried to change Double to _Float with no result..

64-bit integer has at least 18 places of precision, maybe 19 and it depends on it being positive or negative. Each decimal place takes away from how large the number could be. However when computing primes there shouldn't be a need for fractions, or am I wrong?

(That depends on how CURRENCY type was treated by M$ BASIC PDS v7.1 which was actually 32-bit integer.)

https://qb64phoenix.com/qb64wiki/index.php/INTEGER64

The code that Jack showed a short time ago has a number 2^100 that will have to be represented by "string math". So not as much it will "take forever", but QB64(PE) doesn't have any numeric data type at the moment to represent such a value. LOL maybe that's why he said something about "bignum" ...
Reply
#29
@mnrvovrfc
go right ahead and complete the challenge and compute 100 primes using the formula, put your money where your mouth is, I seriously doubt that you would even compute 20 primes
Reply
#30
Actually, modifying your nthprime program slightly with Double extended to _Float it still returns the 7th prime as 129..

Code: (Select All)
_Define A-Z As _FLOAT
Const Pi = Atn(1) * 4
Dim As Long i

For i = 1 To 7
    Print "Number"; i, "Prime"; nprime(i),
    Print "Factorial"; factorial(i)
Next
End

Function factorial## (n As Long)
    Dim As Long i
    Dim As _Float 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 _Float s1, s2, s3
    Dim As _Float 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




Users browsing this thread: 9 Guest(s)