Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge for you...
#41
The line containing the "2.7128F" is a comment, would not get compiled by C/C++ but...

Whoever wrote that code tried to take a shortcut and failed. But a supercomputer and some other programming language would have to adopt the formula of the first post of this topic.
Reply
#42
(04-20-2023, 09:23 PM)bplus Wrote:
(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?

I guess so. You just have to know how to implement that in Julia.
With a simple function is not complicated, Jack's program is. One would have to work through the instructions first.

What I still miss in Julia is the setting with indentation(tabs) and colors. And then how to save the script.
Functions in Julia

[Image: Fibo-in-Julia2023-04-22.jpg]
Reply
#43
Tried to install this Julia "natively" on EndeavourOS (Arch-based Linux) but it wants to install a package that would trash something needed by GIMP so... currently downloading the "generic" version from the authors' web site. Let's see how this goes. However it wouldn't be very useful if it does scientific notation on 99! This formula is just too ridiculous for any programming language to handle, requesting output of very small numbers out of computing with very large ones. Is there a jargon for such a thing?

Edit: this is bad:

Code: (Select All)
julia> print(2*3*4*5*6*7*8*9*10)
3628800
julia> print(2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20)
2432902008176640000
julia> print(2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20*21*22*23*24*25*26*27*28*29*30)
-8764578968847253504
julia>

I did it that way because I don't know it it has a built-in factorial function.

EDIT #2: OK read the manual. But this might not be accurate:

Code: (Select All)
julia> factorial(big(30))
265252859812191058636308480000000

julia> factorial(big(50))
30414093201713378043612608166064768844377641568960512000000000000

julia> factorial(big(99))
933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
Reply
#44
@mnrvovrfc - I don't comprehended that in Julia.

I have a problem with the factorial in Julia. When testing with QB64, the same problem occurs - but somehow inexplicable, because a comparable program works.
Can someone explain the problem with the QB64 program? Thanks!

In Julia:
[Image: Fakultaetproblem-Julia2023-04-22.jpg]

In QB64 the same problem:
Code: (Select All)
'Fakultaetbeispiel, siehe Julia - 22. April 2023

Option _Explicit

Declare Function fakultaet(n As Integer) As _Integer64

Dim As Integer n

Locate 3, 3
Input "Fakultaet(n) -> n: ", n

Locate 3, 5
Print Using "Fakultaet von ### ist: #####"; n, fakultaet(n)

End 'Hauptprogramm

Function fakultaet (n As Integer)

  Dim As _Integer64 fak

  fak = 1
  fak = n * fakultaet(n - 1)
  fakultaet = fak
End Function

That is working. Practically the same program.
Code: (Select All)
'Fakultaetbeispiel 2, siehe Julia - 22. April 2023

Option _Explicit

Declare Function fakultaet(n As Integer) As _Integer64

Dim As Integer n

Locate 3, 3
Input "Fakultaet(n) -> n: ", n

Locate 5, 3
Print Using "Fakultaet von ### ist: #####"; n, fakultaet(n)

End 'Hauptprogramm

Function fakultaet (n As Integer)

  Dim As _Integer64 fak

  'Das ist der Knackpunkt!
  'Ansonsten ist fak offenbar Null
  fak = 1
  If n > 0 Then
    fak = n * fakultaet(n - 1)
  End If

  fakultaet = fak
End Function
Reply
#45
Remove this line. It is not needed in QB64.

Code: (Select All)
Declare Function fakultaet(n As Integer) As _Integer64

Then change the function heading to:

Code: (Select All)
Function fakultaet&& (n As Integer)

In QB64 must use the type sigils to define the return type of a function.

In Julia I guess wrap "n" and "n-1" as "big(n)" and "big(n-1)". I had to use "big()" as suggested in the manual to get factorials larger than 23 or so, that didn't wrap around signed 64-bit integer range.
Reply
#46
And why does it work in the second program? The declaration is exactly the same.

I know this is no longer necessary in QB64, but for me it's for clarity.
Reply
#47
(04-22-2023, 09:30 PM)Kernelpanic Wrote: And why does it work in the second program? The declaration is exactly the same.

I know this is no longer necessary in QB64, but for me it's for clarity.

This kept the program from running infinitely.
Code: (Select All)
    If n > 0 Then
        fak = n * fakultaet(n - 1)
    End If

Though its kinda odd it did stop and print the factorial.

The first program kept subtracting 1 from n with no bottom limit.
b = b + ...
Reply
#48
@Kernelpanic
you need an exit condition, something like this
Code: (Select All)
function fak(n::Int)
      if n==0
              return 1
      else
              return n*fak(n-1)
      end
end

or if you want big factorial

Code: (Select All)
function fak(n::Int)
       if n==0
              return BigInt(1)
       else
              return n*fak(n-1)
       end
end

you could also use BigFloat instead of BigInt

Code: (Select All)
function fak(n::Int)
    f::BigFloat=1
    if n==0
        return f
    else
        for i::Int=1:n
            f*=i
        end
    end
    return f
end

fak(1000000)
8.263931688331240062376646103172666291135347978963873045167775885563379611039583e+5565708
Reply
#49
A program for big factorials (not using strings). It was written to work with QB4.5
Code: (Select All)
DefInt A-S
mx = 32000
Dim f(mx) As Integer
Input "input number"; n
f(mx) = 1
m = mx
For i = 2 To n
    For j = mx To m Step -1
        f(j) = f(j) * i + ic
        ic = f(j) \ 10
        f(j) = f(j) Mod 10
    Next j
    While ic <> 0
        m = m - 1
        f(m) = f(m) + ic
        ic = f(m) \ 10
        f(m) = f(m) Mod 10
    Wend
Next i
For j = m To mx
    Print Using "#"; f(j);
Next j
Print " ="; n; "!"
Reply
#50
@bplu & @jack Thanks. - I suspected that it was due to an explicitly specified termination condition, but it's still not really clear to me why it doesn't work without it.
With each call, n is decremented until n = 0, and then . . . the program gets off track. - Well, it doesn't work without a defined termination condition.

In Julia it works now too. And I have spent a few hours with the manual and trying things like (n::BigInt) etc.

@mnrvovrfc, the result is correct.

[Image: Julia-Big-Int2023-04-23.jpg]
Reply




Users browsing this thread: 35 Guest(s)