Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenges
#31
(06-18-2024, 10:42 PM)bplus Wrote: What numbers between 1 and 1000 can NOT be expressed as the sum of consecutive integers?
BONUS: one number can be expressed 15 different ways as a sum of consecutive numbers what is it and list the ways. Smile


I didn't take time to check the whole list, but it appears using the numbers from 1 to 100, that the powers of 2 are the values which can't be summed.  (2, 4, 8, 16, 32, 64..)

945, and its starting points:      2 10 17 22 35 44 56 61 90 101 132 155 187 314 472
Reply
#32
Bonus points if you can calculate the O(n) eval time of SMcNeill's brutal algorithm.

Code: (Select All)
FOR n = 1 TO 945 '1000
    FOR j = 2 TO INT(n / 2)
        q = n / j
        p = INT(j / 2)
        t = 0
        trigger = 0
        IF (q > p) THEN
            IF ((j MOD 2) <> 0) THEN ' j Odd
                f = 0
                IF (q = INT(q)) THEN
                    trigger = 1
                END IF
            ELSE ' j Even
                f = .5
                IF ((n MOD 2) = 0) THEN ' n Even
                    IF (q - p) <> INT(q - p) THEN
                        IF (q * 2 = INT(q * 2)) THEN
                            trigger = 1
                        END IF
                    END IF
                ELSE ' n Odd
                    IF (q * 2 = INT(q * 2)) THEN
                        trigger = 1
                    END IF
                END IF
            END IF
            IF (trigger = 1) THEN
                PRINT j; ")"; q * j; "=";
                FOR k = q - p + f TO q + p - f
                    PRINT k;
                    t = t + k
                NEXT
                PRINT "="; t
            END IF
        END IF
    NEXT
NEXT
Reply
#33
+1 Steve and KingLeonidas
   

@KingLeonidas your code would benefit from $Console:only so you can see full listing by scrolling up and down.

both of you have code radically different than mine so i will wait a couple days before posting mine, luv to see people's different approaches to same problem.

Since presenting this challenge, i found a way to write a sub (15 lines) that lists the all the ways for a number n; it does have a limit so n up to 1000 works.
BTW it found 5 numbers before 100 that had 5 ways to get consecutive numbers that sum to n.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#34
since we are a page beyond the original challenge post here it is again:
Quote:What numbers between 1 and 1000 can NOT be expressed as the sum of consecutive integers?

ie 15 = 7 + 8
18 = 5 + 6 + 7

so neither one of these are in the set of numbers that can NOT be expressed as a sum of consecutive integers.

BONUS: one number can be expressed 15 different ways as a sum of consecutive numbers what is it and list the ways. Smile

here are some hints i posted elsewhere in case someone else would like to give this a shot.

Quote:hint: 1 + 2 + 3... + n = n(n+1)/2 so sum of first 100 pos integers = 100 * 101 / 2 = 50 * 101 = 5050

my first approach was to eliminate all 2 consecutive sums first then 3, then 4... up to about 40 like sieving for primes, that worked.
then i worked out a way to list all ways to get consecutive numbers to add up to any given number, that too was successful in 15 lines of code.

n + (n + 1) = sum of consecutive numbers
n + (n + 1) + (n + 2) = sum of 3 consecutive numbers
n + (n + 1) + (n + 2) + (n + 3) sum of 4 consecutive numbers

2nd day reply at another forum
Quote:yeah there are 10 from 1 to 1000 inclusive, funny i did not count them before i reread you first reply.

did you find the one that has 15 different ways to sum consecutives to it?
yes the sum of digits of the special number is 18 Smile
i didn't read that correctly the first time sorry.
In fact, what you wrote made no sense to me, but I see and appreciate you are keeping things semi-secret Smile

more hints;
1+2 = 3
2+3 = 5
3+4 = 7
...

so all 2 consecutive number sums are odd.

1+2+3 = 6
2+3+4 = 9
3+4+5 = 12
...

so every other 3 consecutive number sums are even = 3n, n starts at 2

if 2 consecutive sums are even so likely then 4 consecutive sums
1+2+3+4 = 10
2+3+4+5 = 14
3+4+5+6 = 18
...

yeah 2 consecutives already took out all these numbers

so what's interesting to us is sum of odd number consecutive integers

last hint:
Quote:i have been thinking the pattern pretty much makes itself known in first 10 integers, by 20 it is pretty clear and by 100 solid!
but we need something for our computer programming skills to be sharpened so the BONUS number question.

i will post my code sunday, but i wonder if someone will try an approach similar to mine?
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#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

   
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#36
Crop Circles!
Here is: "A Crop of Circles"

Code: (Select All)
_Title "A Crop of Circles" ' b+ 2025-08-14
Screen _NewImage(700, 700, 32): _ScreenMove 315, 0
pow = 2.5
Color , &HFF008800: Cls
Do
    For y = 0 To 700 Step 50
        For x = 0 To 700 Step 50
            dist = ((x - 350) ^ 2 + (y - 350) ^ 2) ^ .5
            FC3 x, y, 16000 / (dist ^ pow), &HFF00BB00
        Next
    Next
    _Display
    _Limit 60
    pow = pow - .001
    If pow < 1 Then Exit Do
Loop

Sub FC3 (cx As Long, cy As Long, r As Long, clr~&)
    Dim As Long r2, x, y ' for Option _Explicit
    If r < 1 Then Exit Sub
    Line (cx - r, cy)-(cx + r, cy), clr~&, BF
    r2 = r * r
    Do
        y = y + 1
        x = Sqr(r2 - y * y)
        Line (cx - x, cy + y)-(cx + x, cy + y), clr~&, BF
        Line (cx - x, cy - y)-(cx + x, cy - y), clr~&, BF
    Loop Until y = r
End Sub
Kick it off with a simple animation!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#37
QBJS Port
Reply
#38
Try one of these in QB64:
https://www.google.com/search?client=ope...=647&dpr=1
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#39
Ok, @bplus, I actually did an original take on your challenge.  This one uses the Wave Function Collapse algorithm to generate a game tilemap from a quasi-randomly generated crop circle pattern:

   

Try it out here:


Attached Files
.zip   circles.zip (Size: 153.78 KB / Downloads: 128)
Reply
#40
Cool, just made my day @dbox!
Last screen was interesting and unexpected.

PS just got home yesterday from major surgery and huge pain in belly from stitches. Found out the heat in my new condo doesn't respond to the thermostat settings, at least getting some heat, I think as temperature staying above 67.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rosetta Code Challenges bplus 15 3,372 04-29-2024, 03:03 AM
Last Post: bplus

Forum Jump:


Users browsing this thread: 1 Guest(s)