Für diejenigen, die gerne Kreuzsummen- oder
Netzwerk-Rätsel
lösen, gibt es hier eine Liste aller gültigen Summen
sortiert nach Zahl, Länge und Ziffern.
Quelltexte in JavaScript und VisualBasic sowie für MSExcel
und awk gibt es weiter unten.
Quelltext JavaScript 1.2
// // This JavaScript 1.2 program finds all combinations of two to nine // digits and calculates each resulting sum. // // 2004-03-26 by Michael Pousen // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. //
// the list containing the results var results = new Array();
// recursive function to find all cobinations // depth = maximum search depth = amount of digits // level = current search depth // lower = lowest digit to add to the combination // found = found digits in integer array function FindSums(depth, level, lower, found) { var i, sum, line; if (level > depth) { // Loop over the array of the found digits and // - build a result line out of them // - calculate their sum. sep = ""; line = ""; sum = 0; for ( i = 1; i <= 9; i++ ) { if ( found[i-1] > 0 ) { sum = sum + i; line = line + sep + i; sep = " + "; } } if ( sum < 10 ) { sum = "0" + sum; } // Emit the resulting line containing length, sum and ciphers. // Lines can later be sorted accordingly. results.push( depth + ": " + sum + " = " + line ); // alternative sorting: //results.push( sum + " (" + depth + ") = " + line ); } else { // Create all still possible combinations of digits. // The lowest digit is defined by the one we had a level before, so we // do not have duplicate tries here. // The highest digits is defined by the amount of digits that follow // until depth is reached. for ( i = lower; i <= 9 - depth + level; i++ ) { // Mark digit as "in use" found[i-1] = 1; // until we add the next one to the combination. FindSums(depth, level + 1, i + 1, found); // Clear flag afterwards. found[i-1] = 0; } } }
// Loop over all possible lengths. var depth; for ( depth = 2 ; depth <= 9; depth++ ) { var found = new Array(0,0,0,0,0,0,0,0,0); FindSums(depth, 1, 1, found); } // Sort and emit results. results.sort(); var i; for ( i = 0; i < results.length; i++ ) { document.write( results[i] + "<br>" ); }
Quelltext Visual Basic .NET 7.1
' ' Dieses Visual Basic .NET 7.1 Programm findet alle Kombinationen von zwei bis neun ' Ziffern und berechnet deren jeweilige Summe. ' ' 2004-03-20 by Besserwisser ' 2004-03-26 output directed to console by Michael Pousen ' ' This program is public domain software. ' It is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. '
Module Module1
' die Ergebnisliste Dim results As Collections.ArrayList
Sub Main() ' Ergebnisse leeren. results = New Collections.ArrayList ' ein Durchlauf für jede der möglichen Längen Dim depth As Integer For depth = 2 To 9 Dim found(9) As Boolean FindSums(depth, 1, 1, found) Next ' Ergebnisse sortieren und ausgeben. results.Sort() Dim key As String For Each key In results Console.WriteLine(key) Next End Sub
' rekursive Funktion zum Finden aller Kombinationen ' depth = maximale Suchtiefe = Anzahl Ziffern ' level = aktuelle Suchtiefe ' lower = niedrigste auszuprobierende Ziffer ' found = gefundene Ziffern als Bool-Array Sub FindSums(ByVal depth As Integer, ByVal level As Integer, ByVal lower As Integer, ByVal found() As Boolean) Dim i As Integer If (level > depth) Then ' Gehe über das Array der gefundenen Ziffern und ' - baue daraus eine Ergebniszeile ' - ermittle dabei die Summe dieser Ziffernkombination. Dim line As String = "" Dim sum As Integer = 0 For i = 1 To 9 If found(i) Then If line > "" Then line &= " + " End If sum += i line &= i.ToString() End If Next ' Wir halten das Ergebnis fest sortiert nach Länge, Summe und Ziffern. results.Add(depth.ToString("0: ") & sum.ToString("00 = ") & line) Else ' alle sinnvoll möglichen Ziffern ausprobieren ' Die Niedrigste wird über die Vorherige bestimmt, so daß wir hier ' keine doppelten Versuche machen. ' Die Höchste wird durch die Anzahl der noch folgenden Ziffern bestimmt, ' obwohl man auf diese Optimierung wohl auch verzichten könnte, ' da die For-Schleife korrekt abbricht. For i = lower To 9 - depth + level ' Gefundene Ziffer notieren, found(i) = True ' bevor die nächste ausprobiert wird. FindSums(depth, level + 1, i + 1, found) ' Merker wieder löschen. found(i) = False Next End If End Sub
End Module
Quelltext Microsoft Excel 8
Attribute VB_Name = "XSums" ' ' This Microsoft Excel 8 macro finds all combinations of two to nine ' digits and calculates each resulting sum. ' ' 2004-03-26 by Michael Pousen ' ' This program is free software; you can redistribute it and/or modify ' it under the terms of the GNU General Public License as published by ' the Free Software Foundation; either version 2 of the License, or ' (at your option) any later version. ' ' This program is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ' GNU General Public License for more details. '
' the current line Dim currLine As Integer
' the starting point Sub Button1_Click() ' Clear results page. Worksheets(1).Activate Range("A1:Z999").Clear Application.Goto Reference:=Range("A1"), Scroll:=True currLine = 0 ' Loop over all possible lengths. Dim depth As Integer For depth = 2 To 9 Call FindSums(depth, 1, 1, Array(False, False, False, False, False, False, False, False, False)) Next ' Sort results. Range("A1", Cells(currLine, 11)).Sort Key1:=Range("A1"), Key2:=Range("B1") End Sub
' recursive function to find all cobinations ' depth = maximum search depth = amount of digits ' level = current search depth ' lower = lowest digit to add to the combination ' found = found digits in boolean array Sub FindSums(ByVal depth As Integer, ByVal level As Integer, ByVal lower As Integer, ByVal found As Variant) Dim i As Integer Dim currCol As Integer If (level > depth) Then ' Loop over the array of the found digits and place them in the current row. currCol = 2 For i = 1 To 9 If found(i - 1) Then ActiveCell.Offset(currLine, currCol).Value = i currCol = currCol + 1 End If Next ' Calculate sum and set length. ActiveCell.Offset(currLine, 1).Value = Application.WorksheetFunction.sum(Range(Cells(currLine + 1, 3), Cells(currLine + 1, depth + 3))) ActiveCell.Offset(currLine, 0).Value = depth currLine = currLine + 1 Else ' Create all still possible combinations of digits. ' The lowest digit is defined by the one we had a level before, so we ' do not have duplicate tries here. ' The highest digits is defined by the amount of digits that follow ' until depth is reached. For i = lower To 9 - depth + level ' Mark digit as "in use" found(i - 1) = True ' until we add the next one to the combination. Call FindSums(depth, level + 1, i + 1, found) ' Clear flag afterwards. found(i - 1) = False Next End If End Sub
Quelltext und Aufruf awk / sort
# # This awk script finds all combinations of two to nine # digits and calculates each resulting sum. # # 2004-03-26 by Michael Pousen # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # awk ' function findsums( depth, level, lower, found, i ) { if (level > depth) { sep = ""; line = ""; sum = 0; for ( i = 1; i <= 9; i ++ ) { if ( found[i] > 0 ) { sum = sum + i; line = line sep i; sep = " + "; } } printf "%d: %2d = %s\n", depth, sum, line; } else { for ( i = lower; i <= 9 - depth + level; i++ ) { found[i] = 1; findsums(depth, level + 1, i + 1, found, 0); found[i] = 0; } } } BEGIN { for ( depth = 2; depth <= 9; depth ++ ) { findsums(depth, 1, 1, found, 0); } } ' </dev/null | sort