-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sweep: Write Collatz Conjecture bench in Python #19
Comments
🚀 Here's the PR! #20See Sweep's progress at the progress dashboard! ⚡ Sweep Basic Tier: I'm using GPT-4. You have 2 GPT-4 tickets left for the month and 3 for the day. (tracking ID:
c46d094aaf )For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets). Tip I'll email you at [email protected] when I complete this pull request! Actions (click)
GitHub Actions✓Here are the GitHub Actions logs prior to making any changes: Sandbox logs for
|
#!/usr/bin/env php | |
<?php | |
const NUMBER = 500000; | |
function collatz($x) { | |
$len = 0; | |
while ($x > 1) { | |
if ($x % 2 == 0) { | |
$x = $x / 2; | |
} else { | |
$x = 3 * $x + 1; | |
} | |
$len++; | |
} | |
return $len; | |
} | |
function findMaxCollatz($to) { | |
$result = [1, 1]; | |
for ($number = 1; $number <= $to; $number++) { | |
$len = collatz($number); | |
if ($len > $result[1]) { | |
$result = [$number, $len]; | |
} | |
} | |
return $result; | |
} | |
(function () { | |
$startTimeMs = floor(microtime(true) * 1000); | |
print_r(findMaxCollatz(NUMBER)); | |
$endTimeMs = floor(microtime(true) * 1000); | |
$durationMs = $endTimeMs - $startTimeMs; | |
echo "Execution time: " . $durationMs . "ms\n"; |
benchmarks/javascript/collatz/MaxSequence.js
Lines 1 to 45 in 5a77198
#!/usr/bin/env node | |
'use strict'; | |
const NUMBER = 500000; | |
function collatz(x) | |
{ | |
var len = 0; | |
while (x > 1) { | |
if (x % 2 == 0) { | |
x = x / 2; | |
} else { | |
x = 3 * x + 1; | |
} | |
len++; | |
} | |
return len; | |
} | |
function findMaxCollatz(to) { | |
var result = [1, 1]; | |
for (var number = 1; number <= to; number++) { | |
var len = collatz(number); | |
if (len > result[1]) { | |
result = [number, len]; | |
} | |
} | |
return result; | |
} | |
(async function() { | |
const startTimeMs = new Date().getTime(); | |
console.log(findMaxCollatz(NUMBER)); | |
const endTimeMs = new Date().getTime(); | |
const durationMs = endTimeMs - startTimeMs; | |
console.log(`Execution time: ${durationMs}ms`); |
benchmarks/ruby/collatz/MaxSequence.rb
Lines 1 to 38 in 5a77198
NUMBER = 500_000 | |
def collats(x) | |
len = 0 | |
while x > 1 do | |
if x % 2 == 0 | |
x = x / 2 | |
else | |
x = 3 * x + 1 | |
end | |
len += 1 | |
end | |
return len | |
end | |
def findMaxCollatz(to) | |
result = [1, 1] | |
for number in 1..to | |
len = collats(number) | |
if len > result[1] | |
result = [number, len] | |
end | |
end | |
return result | |
end | |
startTimeMs = Time.now.to_f * 1000 | |
puts findMaxCollatz(NUMBER) | |
endTimeMs = Time.now.to_f * 1000 | |
durationMs = (endTimeMs - startTimeMs).to_i |
benchmarks/python/primes/Atkin.py
Lines 98 to 107 in 5a77198
def run(): | |
startTimeMs = int(round(time.time() * 1000)) | |
print(find(UPPER_BOUND, PREFIX)) | |
endTimeMs = int(round(time.time() * 1000)) | |
executionTime = endTimeMs - startTimeMs | |
print(f"Execution time: {executionTime}ms") |
Step 2: ⌨️ Coding
Create python/collatz/MaxSequence.py with contents:
• Begin by importing the necessary module for time measurement: `import time`.
• Define a constant `NUMBER` with the value `500000`, similar to the examples in other languages.
• Implement the `collatz` function that takes an integer `x` as input and returns the length of the Collatz sequence for that number. Use a while loop to iterate until `x` becomes 1, applying the Collatz operations as per the examples.
• Implement the `findMaxCollatz` function that takes an integer `to` as input and returns the number and length of the longest Collatz sequence up to `to`. Use a for loop to iterate through each number, calling the `collatz` function, and keep track of the maximum length and corresponding number.
• Add a main block to measure execution time, call the `findMaxCollatz` function with `NUMBER` as the argument, and print the result along with the execution time. Use the `time` module for measuring execution time, similar to the approach in `python/primes/Atkin.py`.
- Running GitHub Actions for
python/collatz/MaxSequence.py
✓ Edit
Check python/collatz/MaxSequence.py with contents:Ran GitHub Actions for 7340e6ce904d658943040c40c4ee02aa66d7e1f1:
Modify python/benchmark.yml with contents:
• Add an entry for the new Collatz Conjecture benchmark script under the Python section. The entry should follow the YAML list format, similar to other entries in the file. The entry should be `- collatz/MaxSequence.py`, ensuring it is properly indented to align with other benchmark script entries.--- +++ @@ -19,3 +19,4 @@ - recursion/Tak.py - primes/Simple.py + - collatz/MaxSequence.py
- Running GitHub Actions for
python/benchmark.yml
✓ Edit
Check python/benchmark.yml with contents:Ran GitHub Actions for a862be27e94a577056e292d7f114653ea74f4bbc:
Step 3: 🔁 Code Review
I have finished reviewing the code for completeness. I did not find errors for sweep/write_collatz_conjecture_bench_in_python
.
🎉 Latest improvements to Sweep:
- New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
- Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
- Use the GitHub issues extension for creating Sweep issues directly from your editor.
💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Details
Implement
collatz/MaxSequence
benchmark in Python, and put it into python/collatz/MaxSequence.py.You MUST implement it exactly in the same way as in other languages examples: php/collatz/MaxSequence.php, javascript/collatz/MaxSequence.js, ruby/collatz/MaxSequence.rb. Add script to python/benchmark.yaml
Here are other examples of out Python code: python/primes/Atkin.py. Use it to se how collect and report execution time.
Checklist
python/collatz/MaxSequence.py
✓ 7340e6c Editpython/collatz/MaxSequence.py
✓ Editpython/benchmark.yml
✓ a862be2 Editpython/benchmark.yml
✓ EditThe text was updated successfully, but these errors were encountered: