Author Topic: Euler's Sum of Powers Conjecture (Disproof) - Rosetta Code  (Read 3257 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
I am sure this could be speeded up but not a long wait here:
Code: QB64: [Select]
  1. _Title "Euler's Sum of Powers Conjecture (Disproof)" ' b+ 2021-05-04
  2. Dim As _Unsigned _Integer64 a, b, c, d, e, max, sumb, sumc, sumd
  3. For c = 1 To 250
  4.     p5(c) = c * c * c * c * c
  5. max = p5(250)
  6. For a = 1 To 250
  7.     Print a 'show progress on slowest integer
  8.     For b = a + 1 To 250
  9.         If p5(a) + p5(b) > max Then Exit For Else sumb = p5(a) + p5(b)
  10.         For c = b + 1 To 250
  11.             If sumb + p5(c) > max Then Exit For Else sumc = sumb + p5(c)
  12.             For d = c + 1 To 250
  13.                 If sumc + p5(d) > max Then Exit For Else sumd = sumc + p5(d)
  14.                 For e = 1 To 250
  15.                     If sumd = p5(e) Then Print a; "^ 5 +"; b; "^ 5 +"; c; "^ 5 +"; d; "^ 5 ="; e; "^ 5": End
  16.                 Next
  17.             Next
  18.         Next
  19.     Next
  20.  

About 2 mins.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Euler's Sum of Powers Conjecture (Disproof) - Rosetta Code
« Reply #1 on: May 05, 2021, 04:02:32 am »
hi bplus :)
if you feel adventurous, you could edit qb64_1.5_win_x64\internal\c\makeline_win.txt and add -O2 after g++ for about 170% speedup
that is if you are running the 64-bit version
to make timing easier, I made a small addition
Code: QB64: [Select]
  1. _Title "Euler's Sum of Powers Conjecture (Disproof)" ' b+ 2021-05-04
  2. Dim As _Unsigned _Integer64 a, b, c, d, e, max, sumb, sumc, sumd
  3. t = Timer
  4. For c = 1 To 250
  5.     p5(c) = c * c * c * c * c
  6. max = p5(250)
  7. For a = 1 To 250
  8.     Print a 'show progress on slowest integer
  9.     For b = a + 1 To 250
  10.         If p5(a) + p5(b) > max Then Exit For Else sumb = p5(a) + p5(b)
  11.         For c = b + 1 To 250
  12.             If sumb + p5(c) > max Then Exit For Else sumc = sumb + p5(c)
  13.             For d = c + 1 To 250
  14.                 If sumc + p5(d) > max Then Exit For Else sumd = sumc + p5(d)
  15.                 For e = 1 To 250
  16.                     If sumd = p5(e) Then
  17.                         Print a; "^ 5 +"; b; "^ 5 +"; c; "^ 5 +"; d; "^ 5 ="; e; "^ 5"
  18.                         Print Timer - t
  19.                         End
  20.                     End If
  21.                 Next
  22.             Next
  23.         Next
  24.     Next
  25.  
[edit] you could also add instead of just -O2 add -O2 -march=native
-march=native might speed it up a bit more than -O2 alone
« Last Edit: May 05, 2021, 04:14:34 am by jack »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Euler's Sum of Powers Conjecture (Disproof) - Rosetta Code
« Reply #2 on: May 05, 2021, 11:50:52 am »
Thanks jack, I was hoping someone could speedup with smarter application of math.

FreeBasic version uses limits and Mod but don't know why.
I tried the limits and didn't make significant difference seems like the EXIT FOR condition covers the limits already?
http://rosettacode.org/wiki/Euler%27s_sum_of_powers_conjecture#FreeBASIC
« Last Edit: May 05, 2021, 11:54:09 am by bplus »

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Euler's Sum of Powers Conjecture (Disproof) - Rosetta Code
« Reply #3 on: May 05, 2021, 01:15:11 pm »
Wow, I just noticed this in the FreeBASIC version:

Code: [Select]
Exit For, For

and it does what you'd expect, pretty slick.  More info: https://www.freebasic.net/wiki/KeyPgExit
Another idea for QB64 in its path to VB.NET glory.


Another interesting thought regarding this, Bill and Steve should chime in.  We know that the N-th order generalization for repeating a line of code without retyping it is a FOR ... TO N loop.  But what is the N-th order generalization for N nested FOR loops, without typing it out?  It can be done with recursion, something like:
Code: [Select]
SUB f(a,b,c,...)
    FOR i=a TO b
        'do stuff
        CALL f( h(a), g(a), ... )
    NEXT
END SUB

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Euler's Sum of Powers Conjecture (Disproof) - Rosetta Code
« Reply #4 on: May 05, 2021, 02:17:50 pm »
bplus, I adapted the freeBasic version that you linked to QB64, it's quite fast
Code: QB64: [Select]
  1. ' http://rosettacode.org/wiki/Euler%27s_sum_of_powers_conjecture#FreeBASIC
  2. ' version 14-09-2015
  3.  
  4. ' some constants calculated when the program is compiled
  5.  
  6. Const max = 250
  7. Const pow5_max = max * max * max * max * max
  8. ' limit x1, x2, x3
  9. Const limit_x1 = (pow5_max / 4) ^ 0.2
  10. Const limit_x2 = (pow5_max / 3) ^ 0.2
  11. Const limit_x3 = (pow5_max / 2) ^ 0.2
  12.  
  13. ' ------=< MAIN >=------
  14.  
  15. Dim As _Unsigned _Integer64 pow5(max), ans1, ans2, ans3
  16. Dim As _Unsigned _Integer64 x1, x2, x3, x4, x5, m1, m2
  17.  
  18.  
  19. For x1 = 1 To max
  20.     pow5(x1) = x1 * x1 * x1 * x1 * x1
  21. Next x1
  22.  
  23. For x1 = 1 To limit_x1
  24.     For x2 = x1 + 1 To limit_x2
  25.         m1 = x1 + x2
  26.         ans1 = pow5(x1) + pow5(x2)
  27.         If ans1 > pow5_max Then Exit For
  28.         For x3 = x2 + 1 To limit_x3
  29.             ans2 = ans1 + pow5(x3)
  30.             If ans2 > pow5_max Then Exit For
  31.             m2 = (m1 + x3) Mod 30
  32.             If m2 = 0 Then m2 = 30
  33.             For x4 = x3 + 1 To max - 1
  34.                 ans3 = ans2 + pow5(x4)
  35.                 If ans3 > pow5_max Then Exit For
  36.                 For x5 = x4 + m2 To max Step 30
  37.                     If ans3 < pow5(x5) Then Exit For
  38.                     If ans3 = pow5(x5) Then
  39.                         Print x1; "^5 + "; x2; "^5 + "; x3; "^5 + "; _
  40.                               x4; "^5 = "; x5; "^5"
  41.                         GoTo skip
  42.                     End If
  43.                 Next x5
  44.             Next x4
  45.             skip:
  46.         Next x3
  47.     Next x2
  48. Next x1
  49.  
  50. Print "done"
  51.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Euler's Sum of Powers Conjecture (Disproof) - Rosetta Code
« Reply #5 on: May 05, 2021, 02:25:21 pm »
OH wow jack! nice improvement, I think I was trying everything with print each line and that dragged on for hours... so it does make a significant difference.

_vince, I missed the Exit For, For that's interesting, as your suggestion for QB64.