Free Republic
Browse · Search
General/Chat
Topics · Post Article

To: TexasGator

I like you and I like your insightful posts, so don’t take this personally. That program is idiotic. It doesn’t get the concept of prime.

Instead of testing all the numbers up to sqrt(n) test all the primes previously found up to sqrt(n), then when you ran out of primes < sqrt(n) start on the next integer. As the n increases, for any positive integer N you pick there is an M such that for n>M my program will be N times as fast as yours/Gemeni’s. And I feel pretty certain my suggestion isalso not the fastest.

Your example supports Stephen Wolfram’s observation that LLMs start out innumerate. They continue sequences of words using markov chain probabilities and if the model is not large enough you see they sound exactly like Kamala Harris word salad.

As a psysics, programmer, lifetime computer engineer and EE/Computer PE, life taught me if you can write a good program to do anything you want, and it does everything the customer requested, you are only a small part of the way there.

1)Requirements are hard to extract from people. If you ask people what they want and do it, most of the time they say “that’s what I said but that isn’t what I want” and change it. (documented study)

2)If it is fast and good it will change the process because they start running it way more times in analysis, such as designing different ways or doing tax returns different ways.

3} Users will conflate the UI with the program logic

4) Every program needs to be tightly integrated with subject matter experts to handle process escapes as times snd facts change.

5) You will constantly have to argue what your work does versus what someone else can promise.


41 posted on 03/16/2025 10:45:00 PM PDT by takebackaustin
[ Post Reply | Private Reply | To 13 | View Replies ]


To: takebackaustin

“I like you and I like your insightful posts, “

Thank you.

“so don’t take this personally.”

I am emotionally detached on this site. Several trolls can attest to that!

“That program is idiotic. “

GEMINI:

It’s unlikely your prime number program was “idiotic,” but it might have been inefficient or have room for improvement. Here’s a breakdown of common issues and how to address them:
Potential Inefficiencies and How to Improve:
* Naive Iteration:
* A basic approach checks divisibility by every number up to n-1. This is slow.
* Improvement: The provided code uses the square root optimization. Checking divisibility up to the square root of n is sufficient. If a number has a divisor greater than its square root, it must also have a divisor smaller than its square root.
* Checking Even Numbers:
* After 2, no other even number is prime. You might have been checking even numbers unnecessarily.
* Improvement: You can optimize by checking only odd numbers after 2.
* Lack of Pre-computed Primes:
* For finding many primes, storing previously found primes and only checking divisibility against them can be much faster.
* Improvement: Implement the Sieve of Eratosthenes, which efficiently finds all primes up to a given limit.
* Inefficient Sieve of Eratosthenes:
* Even the Sieve of Eratosthenes can be implemented poorly.
* Improvement: make sure that the outer loop of the sieve only iterates up to the square root of the upper bound.
* Premature Optimization:
* Sometimes, focusing on micro-optimizations can make the code harder to read and maintain without significant performance gains.
* Improvement: Prioritize clear, correct code first. Profile your code to identify actual bottlenecks before making complex optimizations.
* Incorrect Logic:
* The most “idiotic” error would be a fundamental logic flaw that causes the program to produce incorrect results.
* Improvement: Thoroughly test your code with various inputs, including edge cases.
Example of an Improved Approach (Sieve of Eratosthenes):
def sieve_of_eratosthenes(limit):
primes = [True] * (limit + 1)
primes[0] = primes[1] = False
for p in range(2, int(limit**0.5) + 1):
if primes[p]:
for i in range(p * p, limit + 1, p):
primes[i] = False
return [p for p in range(limit + 1) if primes[p]]

# Find and print the first 100 prime numbers
primes = sieve_of_eratosthenes(542) #542 is slightly higher than the 100th prime.
print(primes[0:100])

Key Improvements in the Sieve:
* It avoids repeated division checks.
* It efficiently marks multiples of primes as non-prime.
In summary, “idiotic” is a harsh word. Focus on understanding where your program could be improved and learn from the process. Prime number generation is a classic problem with many interesting optimization techniques.


64 posted on 03/17/2025 8:07:18 AM PDT by TexasGator (X1.1111'1'./iI11 .I1.11.'1I1.I'')
[ Post Reply | Private Reply | To 41 | View Replies ]

Free Republic
Browse · Search
General/Chat
Topics · Post Article


FreeRepublic, LLC, PO BOX 9771, FRESNO, CA 93794
FreeRepublic.com is powered by software copyright 2000-2008 John Robinson