if (p!=0)
if p <> 0 Then ...
For i as integer = 0 to 2147 - 1 ... Next
Public Function facto(ByVal N As Integer) As String Dim tab(2147) As Integer Dim k As Integer = 1 Dim base As Integer = 1000000 Dim x, y As Integer Dim i As Integer Dim m As Integer Dim p As Integer 'initialisation du tableau de stockage For i = 0 To 2147 - 1 tab(i) = 0 Next tab(2146) = 1 ' Calcul des composantes du résultats et stockage ds le tableau For m = 1 To N p = 0 For i = 1 To k x = (p + m) * tab(2147 - i) y = x Mod base p = x - y / base tab(2147 - i) = y If p = 0 Then tab(2147 + 1 - k) = p End If k = k + 1 Next Next 'affichage du résultat k = 0 While tab(k) = 0 k = k + 1 End While For i = k + 1 To 2147 If tab(i) < 10 Then tab(i) = "00000" ElseIf tab(i) < 100 Then tab(i) = "0000" ElseIf tab(i) < 1000 Then tab(i) = "000" ElseIf tab(i) < 10000 Then tab(i) = "00" ElseIf tab(i) < 100000 Then tab(i) = "0" Else : tab(i) = tab(i) End If Next End Function
cout<<tab[k];cout (http://www.siteduzero.com/tutoriel-3-11131-premier-programme-c-avec-cout-et-cin.html) correspond à writeline si mes souvenirs sont bon en VB. donc c'est un app console.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionla boucle "initialisation du tableau" est faites sur le tableau "tab" et initialise chaque case du tableau ensuite. (et si je m'en souviens bien avec le framework tu n'as pas à initialiser les variables à la création, y'a pas de risque de données récupéré dans la mémoire vive avec d'autre appli)
Public Function facto(ByVal N As Integer) As String Dim tab(100000) As Integer Dim k As Integer = 1 Dim base As Long = 1000000 Dim x As Long Dim y As Long Dim i As Long Dim m As Long Dim p As Long Const TAILLE As Long = 100000 'initialisation du tableau de stockage tab(TAILLE - 1) = 1 ' Calcul des composantes du résultats et stockage ds le tableau For m = 1 To N p = 0 For i = 1 To k x = (p + m * tab(TAILLE - i)) y = x Mod base p = (x - y) / base tab(TAILLE - i) = y Next If p <> 0 Then tab(TAILLE + 1 - k) = p End If k += 1 Next 'affichage du résultat k = 0 While tab(k) = 0 k += 1 End While facto = tab(k) For i = k + 1 To TAILLE - 1 Select case tab(i) Case Is < 10 facto = facto & "00000" & tab(i) Case Is < 100 facto = facto & "0000" & tab(i) Case Is < 1000 facto = facto & "000" & tab(i) Case Is < 10000 facto = facto & "00" & tab(i) Case Is < 100000 facto = facto & "0" & tab(i) Case Else facto = facto & tab(i) End Select Next End Function
Public Function fact1(ByVal A As Integer) As String Dim b As Long ' base Dim longueur_tableau As Integer 'c'est longueur de la factorielle\ 9 Dim F(0) As Long ' tableau contenant le résultat par bloc de 9 Dim m As Long Dim p As Long Dim q As Long Dim i As Integer Dim T As String ' contiendra le résultat ' cas ou A vaut "0" et A négatif(à compléter) If A = "0" Then fact1 = "1" Exit Function End If ' fact1 ne contient rien au départ fact1 = "" 'taille de chaque nombre contenu dans le tableau (soit 9 chiffres maxi) b = 1000000000 ' dimensionne le tableau longueur_tableau = CLng(Int((0.92 + 0.5 * (CUShort(A) + 0.5) * Math.Log(CUShort(A) ^ 2 + CUShort(A) + 1 / 6) - (CUShort(A) + 0.5)) / Math.Log(10)) + 1) longueur_tableau = (longueur_tableau \ 9) + 1 ReDim F(longueur_tableau) ' la dernière entrée du tableau contient la valeur initiale de A F(longueur_tableau) = A ' les calculs For m = A - 1 To 2 Step -1 q = 0 For i = longueur_tableau To 1 Step -1 p = F(i) * m + q q = p \ b F(i) = p - q * b Next i Next m ' on récupère les valeurs à partir du début jusqu'à la fin du tableau ' si on trouve une valeur avec - de 9 chiffres on complète par des zéros For i = LBound(F) To longueur_tableau T = Str$(F(i)) T = Mid$(T, 2, Len(T)) While Len(T) < 9 T = "0" + T End While fact1 &= T Next 'on purge les zéros devant la factorielle While Left(fact1, 1) = "0" And Len(fact1) > 1 fact1 = Replace(fact1, Left(fact1, 1), "", , 1) End While End Function
' The Poor Man's Implementation of the factorial. ' All math is on board, no additional libraries ' are needed. Good enough to compute the factorial ' up to n=10000 in a few seconds. 'Namespace Luschny.Math.Factorial Public Class FactorialPoorMans Public Sub New() End Sub Private A As Long Public Function Factorial(ByVal n As Integer) As String 'If n < 0 Then 'Throw New System.ArgumentException(": n >= 0 required, but was " & n) 'End If If n < 2 Then Factorial = "1" Exit Function End If Dim p As New DecInteger(1) Dim r As New DecInteger(1) A = 1 Dim h As Integer 0, shift As Integer 0, high As Integer = 1 Dim log2n As Integer = CInt(Math.Truncate(System.Math.Floor(System.Math.Log(n) * 1.44269504088896))) While h <> n shift += h h = n >> System.Math.Max(System.Threading.Interlocked.Decrement(log2n), log2n + 1) Dim len As Integer = high high = (h - 1) Or 1 len = (high - len) \ 2 If len > 0 Then p = p * Product(len) r = r * p End If End While r = r * DecInteger.Pow2(shift) Return r.ToString() End Function Private Function Product(ByVal n As Integer) As DecInteger Dim m As Integer = n \ 2 If m = 0 Then Return New DecInteger(A + 2) End If If n = 2 Then Return New DecInteger((A + 2) * (A + 2)) End If Return Product(n - m) * Product(m) End Function End Class ' endOfFactorialPoorMans Friend Class DecInteger Private Const [mod] As Long = 100000000L Private digits As Integer() Private digitsLength As Integer Public Sub New(ByVal value As Long) digits = New Integer() {CInt(value), CInt(value >> 32)} digitsLength = 2 End Sub Private Sub New(ByVal digits As Integer(), ByVal length As Integer) Me.digits = digits digitsLength = length End Sub Public Shared Function Pow2(ByVal e As Integer) As DecInteger If e < 31 Then Return New DecInteger(CInt(Math.Truncate(System.Math.Pow(2, e)))) End If Return Pow2(e \ 2) * Pow2(e - e \ 2) End Function Public Shared Operator *(ByVal a As DecInteger, ByVal b As DecInteger) As DecInteger Dim alen As Integer a.digitsLength, blen As Integer b.digitsLength Dim clen As Integer = alen + blen + 1 Dim digits As Integer() = New Integer(clen - 1) {} For i As Integer = 0 To alen - 1 Dim temp As Long = 0 For j As Integer = 0 To blen - 1 temp = temp + (CLng(a.digits(i)) * CLng(b.digits(j))) + digits(i + j) digits(i + j) = CInt(temp Mod [mod]) temp = temp / [mod] Next digits(i + blen) = CInt(temp) Next Dim k As Integer = clen - 1 While digits(k) = 0 k -= 1 End While Return New DecInteger(digits, k + 1) End Operator Public Overrides Function ToString() As String Dim sb = New System.Text.StringBuilder(digitsLength * 10) sb = sb.Append(digits(digitsLength - 1)) For j As Integer = digitsLength - 2 To 0 Step -1 sb = sb.Append((digits(j) + CInt([mod])).ToString().Substring(1)) Next Return sb.ToString() End Function End Class ' public static void Main (string[] arguments) ' { ' int n = 1000; ' if (arguments.Length != 0) ' { ' n = System.Convert.ToInt32(arguments[0]); ' } ' else ' { ' System.Console.WriteLine("Please give an argument!"); ' } ' FactorialPoorMans f = new FactorialPoorMans(); ' System.Console.WriteLine(n + "! = " + f.Factorial(n)); ' System.Console.ReadLine(); ' }