Sowas ähnliches hab ich mal für Parcival geschrieben, aber ich würde spontan behaupten, dass meins irgendwie einfacher war. Bei Gelegenheit suche ich das mal raus.
Neue Antwort erstellen
Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen
Unsere Datenschutzerklärung wurde aktualisiert. Mit der Nutzung unseres Forums akzeptierst Du unsere Datenschutzerklärung. Du bestätigst zudem, dass Du mindestens 16 Jahre alt bist.
Neu erstellte Beiträge unterliegen der Moderation und werden erst sichtbar, wenn sie durch einen Moderator geprüft und freigeschaltet wurden.
Die letzte Antwort auf dieses Thema liegt mehr als 365 Tage zurück. Das Thema ist womöglich bereits veraltet. Bitte erstellen Sie ggf. ein neues Thema.
Vorherige Beiträge 2
-
-
Da ich zu kgV nicht sehr viel im Internet fand, habe ich mir diese Berechnung mal zur Brust genommen.
Das Ergebnis ist dieses kleine Programm (wieder mal in größter Version entworfen).
kgV - kleinstes gemeinsames Vielfaches
ggT - größter gemeinsamer Teiler
Primfaktor - die Zerlegung wird bei kgV() intern etwas anders genutzt.
Quellcode
- ' Prim und Co.
- ' Autor: Michael Wodrich
- Proc PrimFac
- REM Declare int PFactor[] : PFactor[] = PrimFac(123456)
- REM WhileLoop 0,SizeOf(PFactor[])-1 : If &loop > 0 : Print "·";PFactor[&loop]; : Else : Print PFactor[&loop]; : EndIf : EndWhile : Print ""
- Parameters int n
- Declare int PFac[], cnt, diff, t
- cnt = 0 : diff = 2 : t = 5
- While (n mod 2) = 0 : PFac[cnt] = 2 : inc cnt : n = n \ 2 : EndWhile
- While (n mod 3) = 0 : PFac[cnt] = 3 : inc cnt : n = n \ 3 : EndWhile
- While (t * t) <= n
- While (n mod t) = 0 : PFac[cnt] = t : inc cnt : n = n \ t : EndWhile
- t = t + diff : diff = 6 - diff
- EndWhile
- Case n > 1 : PFac[cnt] = n
- Return PFac[]
- EndProc
- Proc kgV
- REM print "kgV() = ";format$("%d",kgV(12,8))
- REM print "kgV() = ";format$("%d",kgV(62,36))
- Declare float erg, int cnt,x[],xc[]
- cnt = -1
- Proc findInX : Parameters int fac : Var int erg = -1 : Case SizeOf(x[]) < 1 : Return erg : WhileLoop 0,SizeOf(x[])-1 : If x[&loop] = fac : erg = &loop : Break : EndIf : EndWhile : Return erg : EndProc
- Proc addInX : Parameters int fac,fcnt : Declare int n : n = findInX(fac) : If n=-1 : Inc cnt : x[cnt] = fac : xc[cnt] = fcnt : Else : Case xc[n] < fcnt : xc[n] = fcnt : EndIf : EndProc
- Proc sum : var float erg = 0.0 : If SizeOf(x[]) > 0 : erg = x[0]^xc[0] : WhileLoop 1,SizeOf(x[])-1 : erg = erg * x[&loop]^xc[&loop] : EndWhile : EndIf : Return erg : EndProc
- Proc pfc
- Parameters int n
- Declare int fac, fcnt, diff, t
- diff = 2 : t = 5
- fac = 2 : fcnt = 0 : While (n mod 2) = 0 : inc fcnt : n = n \ 2 : EndWhile : Case fcnt > 0 : addInX(2,fcnt)
- fac = 3 : fcnt = 0 : While (n mod 3) = 0 : inc fcnt : n = n \ 3 : EndWhile : Case fcnt > 0 : addInX(3,fcnt)
- While (t * t) <= n
- fcnt = 0 : While (n mod t) = 0 : inc fcnt : n = n \ t : EndWhile : Case fcnt > 0 : addInX(t,fcnt)
- t = t + diff : diff = 6 - diff
- EndWhile
- Case n > 1 : addInX(n,1)
- EndProc
- Select %PCount
- CaseOf 9 : Parameters int a9,b9,c9,d9,e9,f9,g9,h9,i9 : pfc a9 : pfc b9 : pfc c9 : pfc d9 : pfc e9 : pfc f9 : pfc g9 : pfc h9 : pfc i9 : Return sum()
- CaseOf 8 : Parameters int a8,b8,c8,d8,e8,f8,g8,h8 : pfc a8 : pfc b8 : pfc c8 : pfc d8 : pfc e8 : pfc f8 : pfc g8 : pfc h8 : Return sum()
- CaseOf 7 : Parameters int a7,b7,c7,d7,e7,f7,g7 : pfc a7 : pfc b7 : pfc c7 : pfc d7 : pfc e7 : pfc f7 : pfc g7 : Return sum()
- CaseOf 6 : Parameters int a6,b6,c6,d6,e6,f6 : pfc a6 : pfc b6 : pfc c6 : pfc d6 : pfc e6 : pfc f6 : Return sum()
- CaseOf 5 : Parameters int a5,b5,c5,d5,e5 : pfc a5 : pfc b5 : pfc c5 : pfc d5 : pfc e5 : Return sum()
- CaseOf 4 : Parameters int a4,b4,c4,d4 : pfc a4 : pfc b4 : pfc c4 : pfc d4 : Return sum()
- CaseOf 3 : Parameters int a3,b3,c3 : pfc a3 : pfc b3 : pfc c3 : Return sum()
- CaseOf 2 : Parameters int a2,b2 : pfc a2 : pfc b2 : Return sum()
- EndSelect
- Return 0.0
- EndProc
- Proc ggT2 : Parameters quad a,b : Declare quad t : if b>a:t=a:a=b:b=t:endif : Case b=0:Return a : Return ggT2(b,a mod b) : EndProc
- Proc ggT
- REM print "ggT() = ",ggT(1023,99,1071,1029)
- REM print "ggT() = ",ggT(15400,7875,3850)
- REM print "ggT() = ",ggT(62,36)
- Select %PCount
- CaseOf 9 : Parameters quad a9,b9,c9,d9,e9,f9,g9,h9,i9 : Return ggT2(ggT2(ggT2(ggT2(ggT2(ggT2(ggT2(ggT2(a9,b9),c9),d9),e9),f9),g9),h9),i9)
- CaseOf 8 : Parameters quad a8,b8,c8,d8,e8,f8,g8,h8 : Return ggT2(ggT2(ggT2(ggT2(ggT2(ggT2(ggT2(a8,b8),c8),d8),e8),f8),g8),h8)
- CaseOf 7 : Parameters quad a7,b7,c7,d7,e7,f7,g7 : Return ggT2(ggT2(ggT2(ggT2(ggT2(ggT2(a7,b7),c7),d7),e7),f7),g7)
- CaseOf 6 : Parameters quad a6,b6,c6,d6,e6,f6 : Return ggT2(ggT2(ggT2(ggT2(ggT2(a6,b6),c6),d6),e6),f6)
- CaseOf 5 : Parameters quad a5,b5,c5,d5,e5 : Return ggT2(ggT2(ggT2(ggT2(a5,b5),c5),d5),e5)
- CaseOf 4 : Parameters quad a4,b4,c4,d4 : Return ggT2(ggT2(ggT2(a4,b4),c4),d4)
- CaseOf 3 : Parameters quad a3,b3,c3 : Return ggT2(ggT2(a3,b3),c3)
- CaseOf 2 : Parameters quad a2,b2 : Return ggT2(a2,b2)
- CaseOf 1 : Parameters quad a1 : Return a1
- EndSelect
- Return 0
- EndProc
- Proc kgV_aus_ggT
- REM print "kgV_aus_ggT() = ";format$("%d",kgV_aus_ggT(53667,459486))
- REM print "kgV_aus_ggT() = ";format$("%d",kgV_aus_ggT(62,36))
- var float erg = 0.0
- Select %PCount
- CaseOf 9 : Parameters int a9,b9,c9,d9,e9,f9,g9,h9,i9 : erg = a9*b9*c9*d9*e9*f9*g9*h9*i9 / ggT(a9,b9,c9,d9,e9,f9,g9,h9,i9)
- CaseOf 8 : Parameters int a8,b8,c8,d8,e8,f8,g8,h8 : erg = a8*b8*c8*d8*e8*f8*g8*h8 / ggT(a8,b8,c8,d8,e8,f8,g8,h8)
- CaseOf 7 : Parameters int a7,b7,c7,d7,e7,f7,g7 : erg = a7*b7*c7*d7*e7*f7*g7 / ggT(a7,b7,c7,d7,e7,f7,g7)
- CaseOf 6 : Parameters int a6,b6,c6,d6,e6,f6 : erg = a6*b6*c6*d6*e6*f6 / ggT(a6,b6,c6,d6,e6,f6)
- CaseOf 5 : Parameters int a5,b5,c5,d5,e5 : erg = a5*b5*c5*d5*e5 / ggT(a5,b5,c5,d5,e5)
- CaseOf 4 : Parameters int a4,b4,c4,d4 : erg = a4*b4*c4*d4 / ggT(a4,b4,c4,d4)
- CaseOf 3 : Parameters int a3,b3,c3 : erg = a3*b3*c3 / ggT(a3,b3,c3)
- CaseOf 2 : Parameters int a2,b2 : erg = a2*b2 / ggT(a2,b2)
- CaseOf 1 : Parameters int a1 : erg = a1
- EndSelect
- Return erg
- EndProc
- declare long x,y, i
- declare int PFactor[]
- cls
- print "kgV() = ";format$("%d",kgV(53667,459486))
- print "kgV_X() = ";format$("%d",kgV_aus_ggT(53667,459486))
- waitinput
- x = 62 : y = 36
- print "ggT(";x;",";y;") = ",ggT(x,y)
- x = 1023 : y = 99
- print "ggT(";x;",";y;") = ",ggT(x,y)
- x = 1071 : y = 1029
- print "ggT(";x;",";y;") = ",ggT(x,y)
- print "-"
- print "ggT() = ",ggT(1023,99,1071,1029)
- print "ggT() = ",ggT(15400,7875,3850)
- whileloop 2,1000
- print "\nFaktor (";&loop;"): ";
- PFactor[] = PrimFac(&loop)
- for i,0,sizeof(PFactor[])-1
- if i>0 : print "·";PFactor[i]; : else : print PFactor[i]; : endif
- endfor
- Clear PFactor[]
- endwhile
- waitinput
- end
Gruß
Michael Wodrich