Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenges
#35
and the code i used for the solution was
Code: (Select All)
_Title "Sums of consecutive numbers"
' find the numbers between 1 and 1000 that can not be expressed as a sum of a consecutive number.

' ie the sum of 2 consecutives is 2n + 1 from n + (n + 1)
'    the sum of 3                 3n + 3 from n + (n + 1) + (n + 2)
'    the sum of 4                 4n + 6 from n + (n + 1) + (n + 2) + (n + 3)
'    the sum of 5                 5n + 10  from n + (n + 1) + (n + 2) + (n + 3) + (n + 4)
' ...
'    the sum of m consecutives is m*n + m*(m - 1)/2
$Console:Only
Width 140
Dim numbers(1 To 1000) As Integer
m = 2
n = 1
While m < 50
    x = m * n + m * (m - 1) / 2
    While x < 1001
        'Print m, n, x, "zzz..."
        'Sleep
        numbers(x) = numbers(x) + 1
        n = n + 1
        x = m * n + m * (m - 1) / 2
    Wend
    m = m + 1
    n = 1
Wend
Print: Print "And the numbers that can not be expressed as a sum of consecutive integers are:"
top = 1
For i = 1 To 1000
    If numbers(i) > top Then top = numbers(i)
    If numbers(i) = 0 Then Print i;
Next

' !!! result ONLY powers of 2 can not be a sum of consecutive numbers !!!
Print: Print: Print "what numbers can be expressed"; top; "ways?"
For i = 1 To 1000
    If numbers(i) = top Then save = i: Print i,
Next

' !!! result 945 is expressed 15 different ways !!!

Print: Print: Print "Here is the list of ways for consecutive numbers to sum to"; save
consecSets (save)

Sub consecSets (n)
    For i = 2 To 50
        o = n - i * (i - 1) / 2
        If o Mod i = 0 And o > 0 Then
            count = count + 1
            j = o \ i
            Print count; ":"; ts$(j); "+";
            tot = j
            For k = 1 To i - 1
                tot = tot + j + k
                If k = i - 1 Then Print ts$(j + k); " = "; ts$(tot) Else Print ts$(j + k); "+";
            Next
        End If
    Next
End Sub

Function ts$ (n)
    ts$ = _Trim$(Str$(n))
End Function

   
b = b + ...
Reply


Messages In This Thread
Challenges - by bplus - 04-27-2022, 05:21 PM
RE: Challenges - by Pete - 04-27-2022, 05:33 PM
RE: Challenges - by bplus - 04-27-2022, 05:38 PM
RE: Challenges - by Pete - 04-27-2022, 06:00 PM
RE: Challenges - by bplus - 04-27-2022, 06:08 PM
RE: Challenges - by bplus - 04-28-2022, 01:17 AM
RE: Challenges - by Dav - 04-28-2022, 01:26 AM
RE: Challenges - by Pete - 04-28-2022, 01:59 AM
RE: Challenges - by bplus - 05-04-2022, 01:36 AM
RE: Challenges - by Pete - 05-04-2022, 01:51 AM
RE: Challenges - by bplus - 05-04-2022, 01:57 AM
RE: Challenges - by Pete - 05-04-2022, 02:22 AM
RE: Challenges - by bplus - 05-04-2022, 04:10 PM
RE: Challenges - by bplus - 06-18-2022, 01:10 PM
RE: Challenges - by SierraKen - 06-18-2022, 11:32 PM
RE: Challenges - by bplus - 06-19-2022, 01:09 AM
RE: Challenges - by bplus - 03-26-2024, 11:37 PM
RE: Challenges - by CharlieJV - 03-30-2024, 12:02 AM
RE: Challenges - by SMcNeill - 03-27-2024, 12:20 AM
RE: Challenges - by SMcNeill - 03-27-2024, 12:32 AM
RE: Challenges - by bplus - 03-27-2024, 01:24 AM
RE: Challenges - by bplus - 03-30-2024, 02:53 PM
RE: Challenges - by CharlieJV - 03-30-2024, 03:39 PM
RE: Challenges - by bplus - 03-31-2024, 01:33 PM
RE: Challenges - by Petr - 03-31-2024, 02:00 PM
RE: Challenges - by SMcNeill - 03-31-2024, 02:18 PM
RE: Challenges - by Petr - 03-31-2024, 02:23 PM
RE: Challenges - by bplus - 03-31-2024, 03:28 PM
RE: Challenges - by bplus - 06-18-2024, 10:42 PM
RE: Challenges - by SMcNeill - 06-19-2024, 04:32 AM
RE: Challenges - by SMcNeill - 06-19-2024, 04:45 AM
RE: Challenges - by KingLeonidas - 06-19-2024, 09:42 AM
RE: Challenges - by bplus - 06-19-2024, 01:59 PM
RE: Challenges - by bplus - 06-21-2024, 07:31 PM
RE: Challenges - by bplus - 06-23-2024, 03:07 PM



Users browsing this thread: 2 Guest(s)