PlummersSoftwareLLC / Primes

Prime Number Projects in C#/C++/Python
https://plummerssoftwarellc.github.io/PrimeView/
2.44k stars 575 forks source link

PHP Solution 2 with Bitwise storage. #893

Closed sqonk closed 1 year ago

sqonk commented 1 year ago

A ChatGPT-assisted solution for PHP with impressive results.

Description

This solution attempts to boost the performance out of PHP significantly, by using anything that will allow the interpreter and the Opcache JIT to precompile where possible.

The prime count storage utilises bitwise operations, as with many other high performance solutions for other languages. However, PHP has a limitation with regard to its type handling, and the maximum size it can allocate for a single integer. And so in my experiments simply replacing an array of bools with a singular int will only output the correct count for a sieve of 100 or 10.

This solution instead runs a series of ints as storage for every 100 units, storing the whole lot in an array.

Finally, of particular note (especially after the latest episode of Dave's Garage) is the core algorithm in this solution is heavily influenced and aided by ChatGPT, which assisted over many iterations of trial and error in producing a workable solution.

@rbergen I believe this solution to be both faithful and inline with the base algorithm. Please glance over the main class, if you feel so inclined, to confirm.

Contributing requirements

rbergen commented 1 year ago

@sqonk Thank you for your submission.

I've reviewed it, and I see the following:

As the solution does not currently implement a functioning prime sieve, I can't merge this. I'll leave it up to you whether you want to change the PR to make the solution work, or close it. If you do want to move forward with submitting a bit-mapped PHP solution, then please:

Finally, in your PR description, you refer to Dave's last video. I'd like to note that it is true that he created a number of prime sieve implementations with the aid of ChatGPT. However, those implementations were not vetted for inclusion in this project, as that wasn't the point of the video. That means they should not be used as a reference for a "faithful, base" implementation.

sqonk commented 1 year ago

@rbergen Thank you for your comprehensive feedback. I will review and see what can be done.

sqonk commented 1 year ago

I am going to close this off for now. Besides the faults you mention, I have found another critical flaw in the algorithm. I fixed it for the sake of the exercise but not without the cost of the speed.

I am exploring other solutions but they are likely to be different enough that they should be submitted under a seperate request.