Etwas Mathe f.Interessenten zum WE
12.09.2015 04:46:46
Luc:-?
habe eine UDF zum Collatz-Problem, auch als Syracuse-Vermutung bekannt, geschrieben. Mit ihr kann, wie nachfolgd dargestellt, sowohl die Anzahl der Elemente bis zum erstmaligen Erreichen der 1 (als Teil des sich dann wiederholenden 4-2-1-Kreises) ermittelt als auch die Collatz-Folge ausgegeben wdn. Außerdem kann auch mit anderen (ungeraden) Faktoren als die originale 3 (Defaultwert für Arg3) experimentiert wdn.
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | |
1 | 1 | 1 | 1 | 1 | |||||||||||||||||||
2 | 2 | 2 | 1 | 2 | 1 | ||||||||||||||||||
3 | 8 | 16 | 4 | 3 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||||||||||||
4 | 3 | 4 | 1 | 4 | 2 | 1 | |||||||||||||||||
5 | 6 | 16 | 2 | 5 | 16 | 8 | 4 | 2 | 1 | ||||||||||||||
6 | 9 | 16 | 5 | 6 | 3 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |||||||||||
7 | 17 | 52 | 6 | 7 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |||
8 | 4 | 8 | 1 | 8 | 4 | 2 | 1 | ||||||||||||||||
9 | 20 | 52 | 9 | 9 | 28 | 14 | 7 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 |
10 | 7 | 16 | 3 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |||||||||||||
11 | 15 | 52 | 4 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |||||
12 | 10 | 16 | 6 | 12 | 6 | 3 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||||||||||
13 | 10 | 40 | 2 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||||||||||
14 | 18 | 52 | 7 | 14 | 7 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||
15 | 18 | 160 | 8 | 15 | 46 | 23 | 70 | 35 | 106 | 53 | 160 | 80 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||
16 | 5 | 16 | 1 | 16 | 8 | 4 | 2 | 1 | |||||||||||||||
17 | 13 | 52 | 2 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |||||||
18 | 21 | 52 | 10 | 18 | 9 | 28 | 14 | 7 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 |
19 | 21 | 88 | 4 | 19 | 58 | 29 | 88 | 44 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 |
20 | 8 | 20 | 1 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||||||||||||
21 | 8 | 64 | 2 | 21 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ||||||||||||
22 | 16 | 52 | 5 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||||
23 | 16 | 160 | 6 | 23 | 70 | 35 | 106 | 53 | 160 | 80 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | ||||
24 | 11 | 24 | 1 | 24 | 12 | 6 | 3 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |||||||||
25 | 24 | 88 | 7 | 25 | 76 | 38 | 19 | 58 | 29 | 88 | 44 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 |
26 | 11 | 40 | 3 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |||||||||
27 | 112 | 9 232 | 78 | 27 | 82 | 41 | 124 | 62 | 31 | 94 | 47 | 142 | 71 | 214 | 107 | 322 | 161 | 484 | 242 | 121 | 364 | 182 | 91 |
28 | 19 | 52 | 8 | 28 | 14 | 7 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |
29 | 19 | 88 | 2 | 29 | 88 | 44 | 22 | 11 | 34 | 17 | 52 | 26 | 13 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |
30 | 19 | 160 | 9 | 30 | 15 | 46 | 23 | 70 | 35 | 106 | 53 | 160 | 80 | 40 | 20 | 10 | 5 | 16 | 8 | 4 | 2 | 1 | |
31 | |||||||||||||||||||||||
32 |
Weitergehende Infos zur Problematik sind auf Wikipedia zu finden! Hier noch die UDF:
Rem Liefert Anzahl iterativer Berechnungen bis FolgenWert=1 (inkl Arg1)
' alternativ kann d.Collatz-Folge ab Start- bis Endwert ausgegeb wdn.
' Achtung! Fkt kn auch in Vektoren or Matrizen (ZBereiche) angeordnet
' Werte varbeit, aber nur Folgen aus Vektoren auf ZBereiche abbilden;
' negat/gerad Arg3 wird auf d.nächstgröß ungerade gesetzt! Wenn 1 er-
' reicht wird, bricht d.Berechn ab, d.h., d.Kreis 4-2-1 ist erreicht.
' Vs1.1 -LSr:CyWorXxl -cd:20150910 -1pub:20150912herber -lupd:20150910n
Function Collatz(Startwert, Optional ByVal Folge As Boolean, _
Optional ByVal Faktor As Integer = 3)
Dim ix As Long, iz As Long, lz As Long, n As Long, nix As Long, nx As Long, _
nz As Long, isMx As Boolean, nw, nWerte As Variant, wf As WorksheetFunction
On Error GoTo fx: Set wf = WorksheetFunction: Faktor = Faktor + 1 - Faktor Mod 2
If IsArray(Startwert) Then
If TypeName(Startwert) = "Range" Then nWerte = _
wf.Transpose(wf.Transpose(Startwert)) Else nWerte = Startwert
On Error Resume Next
If IsError(LBound(nWerte, 2)) Then
ElseIf Folge And UBound(nWerte, 2) + 1 - LBound(nWerte, 2) = 1 Then
nWerte = wf.Transpose(nWerte)
Else: isMx = True
End If
On Error GoTo fx
If isMx Then
ix = LBound(nWerte, 2): iz = LBound(nWerte, 1)
lz = iz: nz = UBound(nWerte, 1) + 1 - lz
Else: ix = LBound(nWerte)
End If
For Each Startwert In nWerte
n = Startwert: nix = 0: GoSub nv: nx = wf.Max(nx, nix)
If isMx Then
nWerte(iz, ix) = Collatz
iz = (iz + 1 - lz) Mod nz + lz: ix = ix + 1 - Sgn(iz - lz)
Else: nWerte(ix) = Collatz: ix = ix + 1
End If
Next Startwert
If Folge And Not isMx Then
ix = LBound(nWerte)
For Each nw In nWerte
If UBound(nw) Sgn(Startwert)
n = 2 ^ -1 * n * (2 * Faktor) ^ (n Mod 2) + (n Mod 2) * Sgn(Startwert)
nix = nix + 1: If Folge Then ReDim Preserve nw(nix): nw(nix) = n
Wend
Collatz = IIf(Folge, nw, nix + Abs(Sgn(Startwert)))
If Not IsEmpty(nWerte) Then Return
End If
fx: If CBool(Err.Number) Then Collatz = CVErr(xlErrNA)
Set wf = Nothing
End Function
Viel Spaß beim Experimentieren!Gruß+schöWE, Luc :-?