Page 1 of 1

Funtions for variations, permutations and combinations

Posted: Fri Oct 07, 2005 6:23 pm
by Psychophanta
Code updated For 5.20+

Code: Select all

Procedure Permutaciones_de_n_elementos(n) ; o lo que es lo mismo, factorial de n
  ; Esta función hace: n!
  r=1:While n>1:r*n:n-1:Wend
  ProcedureReturn r
EndProcedure

; Procedure Permutaciones_con_repeticion_de_n_elementos(r1,[r2,r3,r4....]); <- can't be done until optional parameters are working
;
; Concepto.-
;
; Si tengo n objetos { a1 , a2 , ..., an } , los puedo colocar ordenadamente de manera que se repitan r1 veces el primero, r2 veces el segundo, ..., y rn veces el n-simo, formando grupos ordenados que reciben el nombre de permutaciones con repetición.
;
; Número.-
;
; El número de permutaciones con repetición de estos n elementos distintos, teniendo cada grupo k elementos a causa de las repeticiones (siendo k = r1 + r2 + ...+ rn ), se denota por Pkr1,r2...rn y equivale a:
;
; Pk r1,r2...rn = n! / (r1! * r2! * ... * rn!)
; EndProcedure

Procedure Variaciones_de_m_elementos_tomados_de_n_en_n(m,n)
  ; Esta función hace: m! / (m - n)!
  m=Abs(m):n=Abs(n)
  r=1:While n:r*m:m-1:n-1:Wend
  ProcedureReturn r
EndProcedure

Procedure Combinaciones_de_m_elementos_tomados_de_n_en_n(m,n)
  ; Esta función hace: m! / (m - n)! / n!
  m=Abs(m):n=Abs(n)
  If 2*n>m:n=m-n:EndIf
  r.f=1:While n:r.f*m/n:m-1:n-1:Wend
  ProcedureReturn r.f
EndProcedure

rows=29; <- Number of rows, this is, the height of the triangle of Tartaglia

For t=0 To rows-1
  For g=0 To t
    Debug Combinaciones_de_m_elementos_tomados_de_n_en_n(t,g)
  Next
  Debug "------------------"
Next

This is to get numerical combinations numbers. Very useful for lots of things for maths calculations: probabilistic stuff, real numbers' sucesions general terms calculation, Newton binome calculation, etc
In the example i get Triangle of Tartaglia (aka Triangle of Pascal) calculation.