FreeBASIC マニュアルのトップに戻る

FreeBASIC PARI/GP

目次→フォーラム→FreeBASIC→補足prime factorization←オリジナル・フォーラム

PARI/GP 左にメニュー・フレームが表示されていない場合は、ここをクリックして下さい

←リンク元に戻る プログラム開発関連に戻る

素因数分解  このページは、「素因数分解を高速に実行する方法」をフォーラムで問い合わせたところ、srvaldez さんに、PARI/GP というライブラリの存在と、FreeBASIC での利用方法を教えていただきました。
 64ビット版 libpari.dll とサンプル・プログラム(libPari.7z)は、下記でダウンロードできます。
https://u.pcloud.link/publink/show?code=XZPDSI0ZHDWy33Q3eWfYQgGyIb3VrHhYL6YX

注意:FreeBASIC のコンパイラは、64ビット版を使います。

 PARI/GP について(シニアエンジニアの庵)
https://sehermitage.web.fc2.com/etc/parigp.html
 数値計算を高速にするだけのライブラリですが、その速度が尋常ではありません。

 PARI/GP の WEB デモ Try PARI/GP in your browser
https://pari.math.u-bordeaux.fr/gpwasm.html
で、例えば下記の結果が瞬時に表示されて感激しました。
factor(717985416201895737890248682491)
factor(666070876152512717106070950186603615630383990358329)


素因数分解

pari4jp2.bas
'prime factorization
'srvaldez ≫ Sep 20, 2024 17:26
'https://www.freebasic.net/forum/viewtopic.php?p=304967#p304967
'the 64-bit dll plus a rough example
'https://u.pcloud.link/publink/show?code=XZPDSI0ZHDWy33Q3eWfYQgGyIb3VrHhYL6YX
'2024/09/23

'★★★★★★★★★★★★★★★★★★★★★★★★★★★
'★★★ FreeBASIC 64ビットを使ってコンパイルします ★★
'★★★★★★★★★★★★★★★★★★★★★★★★★★★

#Pragma Once
#Inclib "pari"
#Include "crt\stdio.bi"

Extern "C"

Type pari_ulong As ULongInt
Type GEN As LongInt Ptr
Type pariFILE
	file As FILE Ptr
	As LongInt Type
	Name As Const ZString Ptr
	prev As pariFILE Ptr
	Next As pariFILE Ptr
End Type

Declare Sub pari_init(ByVal parisize As UInteger, ByVal maxprime As pari_ulong)
Declare Sub pari_close()
Declare Sub pari_print_version()
Declare Function geval(ByVal x As GEN) As GEN
Declare Function setdefault(ByVal s As Const ZString Ptr, ByVal v As Const ZString Ptr, ByVal flag As LongInt) As GEN
Declare Function strtoGENstr(ByVal s As Const ZString Ptr) As GEN
Declare Function setrealprecision(ByVal n As LongInt, ByVal prec As LongInt Ptr) As Long '有効桁数を指定
Declare Function GENtostr(ByVal x As GEN) As ZString Ptr
Declare Function stoi(ByVal x As LongInt) As GEN
Declare Function rtodbl(ByVal x As GEN) As Double
Declare Function dbltor(ByVal x As Double) As GEN
Declare Function glog(ByVal x As GEN, ByVal prec As LongInt) As GEN
Declare Function mpadd(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mpsub(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mpmul(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mpdiv(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mpsin(ByVal x As GEN) As GEN
Declare Function mpcos(ByVal x As GEN) As GEN
Declare Function mptan(ByVal x As GEN) As GEN
Declare Function gsin(ByVal x As GEN, ByVal prec As LongInt) As GEN
Declare Function gcos(ByVal x As GEN, ByVal prec As LongInt) As GEN
Declare Function gtan(ByVal x As GEN, ByVal prec As LongInt) As GEN
Declare Function gasin(ByVal x As GEN, ByVal prec As LongInt) As GEN
Declare Function gacos(ByVal x As GEN, ByVal prec As LongInt) As GEN
Declare Function gatan(ByVal x As GEN, ByVal prec As LongInt) As GEN
Declare Function gadd(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function gsub(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function gmul(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function gdiv(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mului(ByVal x As pari_ulong, ByVal y As GEN) As GEN
Declare Function mulsi(ByVal x As LongInt, ByVal y As GEN) As GEN
Declare Function gp_fileopen(ByVal s As ZString Ptr, ByVal mode As ZString Ptr) As Long
Declare Sub gp_fileclose(ByVal n As LongInt)
Declare Function gp_fileread(ByVal n As LongInt) As GEN
Declare Function gp_read_file(ByVal s As Const ZString Ptr) As GEN
Declare Sub output_ Alias "output"(ByVal x As GEN)
Declare Sub outmat(ByVal x As GEN)
Declare Function gp_input() As GEN
Declare Function gcmp(ByVal x As GEN, ByVal y As GEN) As Long
Declare Function abscmpui(ByVal x As pari_ulong, ByVal y As GEN) As Long
Declare Function lexcmp(ByVal x As GEN, ByVal y As GEN) As Long
Declare Function gisexactzero(ByVal g As GEN) As GEN
Declare Function isexactzero(ByVal g As GEN) As Long
Declare Function gprecision(ByVal x As GEN) As Long
Declare Function gpserprec(ByVal x As GEN, ByVal v As LongInt) As GEN
Declare Function dblexpo(ByVal x As Double) As Long
Declare Function dblmantissa(ByVal x As Double) As pari_ulong
Declare Function algtype(ByVal al As GEN) As Long
Declare Function type0(ByVal x As GEN) As GEN
End Extern

Function whattype(ByVal n As GEN) As String
	Dim As ZString Ptr s=Callocate(16)
	Dim As String t
	Dim As GEN a
	a=type0(n)
	s=GENtostr(a)
	t=*s
	DeAllocate(s)
	Return t
End Function

Function geneval(ByRef s As String) As GEN
	Dim As GEN a, b
	a=strtoGENstr(s)
	b=geval(a) 
	Return b
End Function

'引数を素因数分割して、結果を表示する
Sub primefactors(ByRef number As String)
   
   Dim As GEN a, b
   Dim As ZString Ptr s=Callocate(1024*1024*4)

   Dim STARTT As Long
   Dim ENDTIME As Long
   Dim Minut As Integer
   
   STARTT=Val(Left(Time,2))*3600+Val(Mid(Time,4,2))*60+Val(Right(Time,2))
   
   pari_init(800000000,500000)
   
   *s="factor(" & number & ")"
   a=strtoGENstr(s)    'convert to GEN string
   b=geval(a)          'eval the string in a
   s=GENtostr(b)       'convert GEN to string
   Print "factor(" & number & ") = ";*s
   
   pari_close()
   DeAllocate(s)

   ENDTIME = Val(Left(Time,2))*3600+Val(Mid(Time,4,2))*60+Val(Right(Time,2))
   Minut=(ENDTIME-STARTT)\60
   Print Using "Processing time: ### minutes ## seconds"; Minut; (ENDTIME-STARTT)-Minut*60
   Print Using "処理時間は ### 分 ## 秒"; Minut; (ENDTIME-STARTT)-Minut*60
   Print "***********************************************"
End Sub


'***********************************
Dim Suuchi As String

'以下に素因数分解したい数字を記入します。
primefactors("121439531096594251777")
primefactors("717985416201895737890248682491")
primefactors("996796498818862150784546058216343744333180483201")

Do
   Print "任意の整数を入力して下さい。"
   Print "数字以外の文字を入力すると終了します。"
   Line Input Suuchi
   
   If Str(ValInt(Left(Suuchi,9)))<>Left(Suuchi,9) Then
      Print "何かキー入力で終了します。"
      Sleep
      End
      
   Else
      primefactors(Suuchi)
   End If
Loop

フィボナッチ・平方根・階乗

フィボナッチ
階乗
 その他の PARI/GP の関数の事例です。
https://freebasic.net/forum/viewtopic.php?t=31706

usingLibpariJP2.bas
'using libpari
'by srvaldez ≫ Jun 09, 2022 4:47
'https://freebasic.net/forum/viewtopic.php?t=31706

'★★★★★★★★★★★★★★★★★★★★★★★★★★★
'★★★ FreeBASIC 64ビットを使ってコンパイルします ★★
'★★★★★★★★★★★★★★★★★★★★★★★★★★★

#Pragma Once
#Inclib "pari"

Extern "C"

Type pari_ulong As ULongInt
Type GEN As LongInt Ptr

Declare Sub pari_print_version()
Declare Function geval(ByVal x As GEN) As GEN
Declare Function setdefault(ByVal s As Const ZString Ptr, ByVal v As Const ZString Ptr, ByVal flag As LongInt) As GEN
Declare Function strtoGENstr(ByVal s As Const ZString Ptr) As GEN
Declare Function setrealprecision(ByVal n As LongInt, ByVal prec As LongInt Ptr) As LongInt
Declare Sub pari_init(ByVal parisize As UInteger, ByVal maxprime As pari_ulong)
Declare Function GENtostr(ByVal x As GEN) As ZString Ptr
Declare Function stoi(ByVal x As LongInt) As GEN
Declare Function mpadd(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mpsub(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mpmul(ByVal x As GEN, ByVal y As GEN) As GEN
Declare Function mpdiv(ByVal x As GEN, ByVal y As GEN) As GEN
End Extern

Dim As GEN a, b, c, e, f
Dim As ZString Ptr s=Callocate(1024*1024)
Dim As LongInt prec, flag
Dim As Integer i

pari_init(8000000,500000)
pari_print_version()

setrealprecision(50, @prec) '有効桁数を指定
Print

f=setdefault("seriesprecision", "12", flag)
*s="fibonacci(1007)"
a=strtoGENstr(s)    'convert to GEN string
b=geval(a)          'eval the string in a
s=GENtostr(b)       'convert GEN to string
Print
Print "フィボナッチ(1007) = ";*s
'Print "fibonacci(1007) = ";*s
Print
*s="sin(x)"
a=strtoGENstr(s)    'convert to GEN string
c=geval(a)          'eval the string in a
s=GENtostr(c)       'convert GEN to string
Print "sin(x) = ";*s
Print
*s="Pi"
a=strtoGENstr(s)    'convert to GEN string
c=geval(a)          'eval the string in a
s=GENtostr(c)       'convert GEN to string
Print "Pi = ";*s
Print
Print "2 〜10の平方根を 表示"
Print "evaluate a tiny program: for(i=2, 10, print(sqrt(i)))"
Print
*s="for(i=2, 10, print(sqrt(i)))"
a=strtoGENstr(s)    'convert to GEN string
c=geval(a)          'eval the string in a
Print
Print "何かキー入力すると、フィボナッチ数列の1 〜100と50 の階乗を表示します"
Sleep

Print "フィボナッチ数列の1 〜100を 表示"
For i=1 To 100
   *s="fibonacci(" & Str(i) & ")"
   a=strtoGENstr(s)    'convert to GEN string
   b=geval(a)          'eval the string in a
   s=GENtostr(b)       'convert GEN to string
   Print "フィボナッチ(" & i & ") = ";*s
Next i

Print
Print

*s="50!"
a=strtoGENstr(s) 'convert to GEN string
b=geval(a)       'eval the string in a
s=GENtostr(b)    'convert GEN to string
Print "50! = ";*s

DeAllocate(s)
Sleep
 
補足 に戻る
←リンク元に戻る プログラム開発関連に戻る
ページ歴史:2024-10-26
日本語翻訳:WATANABE Makoto、原文著作者:srvaldez , 2024-09-20 17:26

ホームページのトップに戻る

表示-非営利-継承