Differenze tra le versioni di "Massimo Comun Divisore di due numeri con le sole risorse di Gambas"

Da Gambas-it.org - Wikipedia.
(Creata pagina con "Per trovare il ''[https://it.wikipedia.org/wiki/Massimo_comun_divisore Massimo Comun Divisore]'' di due numeri mediante le sole risorse di Gambas, è possibile adottare il seg...")
 
 
Riga 1: Riga 1:
 
Per trovare il ''[https://it.wikipedia.org/wiki/Massimo_comun_divisore Massimo Comun Divisore]'' di due numeri mediante le sole risorse di Gambas, è possibile adottare il seguente codice che sfrutta il cosiddetto [https://it.wikipedia.org/wiki/Algoritmo_di_Euclide Algoritmo di Euclide]:
 
Per trovare il ''[https://it.wikipedia.org/wiki/Massimo_comun_divisore Massimo Comun Divisore]'' di due numeri mediante le sole risorse di Gambas, è possibile adottare il seguente codice che sfrutta il cosiddetto [https://it.wikipedia.org/wiki/Algoritmo_di_Euclide Algoritmo di Euclide]:
  '''Public''' Sub Main()
+
  Public Sub Main()
 
 
  Dim s As String
 
  Dim ii As New Integer[]
 
  Dim a, b, r As Integer
 
 
    
 
    
 +
  Dim s As String
 +
  Dim ii As New Integer[]
 +
  Dim a, b, r As Integer
 +
 
   Do
 
   Do
 
     Print "Inserisci un numero:"
 
     Print "Inserisci un numero:"
Riga 24: Riga 24:
 
   Print "Il MASSIMO COMUNE DIVISORE tra "; a; " e "; b; " è: "; ii[1]
 
   Print "Il MASSIMO COMUNE DIVISORE tra "; a; " e "; b; " è: "; ii[1]
 
    
 
    
  '''End'''
+
  End
 +
 
   
 
   
 +
Private Function Euclide(nn As Integer[])  <FONT Color=gray>' ''Adottiamo l'algoritmo di Euclide''</font>
 
   
 
   
'''Private''' Function Euclide(nn As Integer[])  <FONT Color=gray>' ''Adottiamo l'algoritmo di Euclide''</font>
+
  Dim i As Integer
 
 
  Dim i As Integer
 
 
    
 
    
 
   i = nn[0] Mod nn[1]
 
   i = nn[0] Mod nn[1]
Riga 39: Riga 39:
 
   Wend
 
   Wend
 
    
 
    
  '''End'''
+
  End

Versione attuale delle 16:23, 21 giu 2024

Per trovare il Massimo Comun Divisore di due numeri mediante le sole risorse di Gambas, è possibile adottare il seguente codice che sfrutta il cosiddetto Algoritmo di Euclide:

Public Sub Main()
 
  Dim s As String
  Dim ii As New Integer[]
  Dim a, b, r As Integer

  Do
    Print "Inserisci un numero:"
    Input s
    ii.Push(Val(s))
  Loop Until ii.Count = 2
  a = ii[0]
  b = ii[1]
  
  If ii[0] < ii[1] Then
    r = ii[0]
    ii[0] = ii[1]
    ii[1] = r
  Endif
  
  Euclide(ii)
  
  Print "Il MASSIMO COMUNE DIVISORE tra "; a; " e "; b; " è: "; ii[1]
  
End


Private Function Euclide(nn As Integer[])   ' Adottiamo l'algoritmo di Euclide

  Dim i As Integer
 
  i = nn[0] Mod nn[1]
  
  While i <> 0
    nn[0] = nn[1]
    nn[1] = i
    i = nn[0] Mod nn[1]
  Wend
  
End