Re: The joy of FORTRAN

Liste des GroupesRevenir à col misc 
Sujet : Re: The joy of FORTRAN
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : alt.folklore.computers comp.os.linux.misc
Date : 06. Mar 2025, 15:28:37
Autres entêtes
Organisation : Stefan Ram
Message-ID : <primes-20250306152532@ram.dialup.fu-berlin.de>
References : 1 2 3 4 5 6
antispam@fricas.org (Waldek Hebisch) wrote or quoted:
critique of decomposed PrimeGenerator.  Personally I find
the Knuth-based pre-decomposed version the clearest one:
- it is obviously correct once one gets loop invariants
- it is compact enough that loop invariants can be easily guessed.

  I talked about this with my old buddy, the chatbot,
  and here's our attempt (in Python):

# Prime Number Generator
# ======================
# This program generates the first `n` prime numbers using an iterative approach.
#
# Algorithm Overview:
# -------------------
# The algorithm generates prime numbers by iteratively testing candidate numbers
# for primality. It maintains two key data structures:
#
# 1. A list of discovered primes (`primes`): This list stores all prime numbers
#    found so far.
#
# 2. A list of multiples of these primes (`multiples`): This list tracks multiples
#    of previously discovered primes to help determine whether a candidate number
#    is composite (not prime).
#
# The algorithm works as follows:
# - Start with the first prime number, 2.
# - Test each subsequent odd number (skipping even numbers, which are not prime).
# - For each candidate number:
#   - If it equals the square of the largest discovered prime, mark it as composite.
#   - Otherwise, check if it is divisible by any previously discovered primes.
# - If no divisors are found, the candidate is prime and is added to the list of primes.
#
# This approach avoids unnecessary checks (e.g., skipping even numbers) and dynamically
# updates multiples as new primes are discovered. While not as efficient as sieve-based
# algorithms for generating large ranges of primes, it is well-suited for generating a
# specific number of primes (`n`).

def generate_primes(n):
    """
    Generate the first `n` prime numbers using an iterative approach.

    Args:
        n (int): The number of prime numbers to generate.

    Returns:
        list: A list containing the first `n` prime numbers.

    Algorithm Details:
    ------------------
    - The function starts with the first prime number (2) and iteratively tests
      odd numbers for primality.
    - It uses a helper function `is_prime` to test each candidate number against
      previously discovered primes and their multiples.
    - The process continues until `n` primes have been found.
    """
    # Step 1: Handle edge cases where `n` is less than or equal to 0.
    if n <= 0:
        return []  # No primes to generate for invalid input.

    # Step 2: Initialize the list of primes with the first prime number (2).
    primes = [2]

    # Step 3: Initialize a list to track multiples of discovered primes.
    #         This helps in marking non-prime (composite) numbers efficiently.
    multiples = [2]

    # Step 4: Begin testing candidate numbers starting from 3 (the next odd number).
    candidate = 3

    # Step 5: Continue testing candidates until we have found `n` primes.
    while len(primes) < n:
        # Check if the current candidate is prime using a helper function.
        if is_prime(candidate, primes, multiples):
            # If it is prime, add it to the list of primes.
            primes.append(candidate)

        # Increment the candidate by 2 to skip even numbers (which are not prime).
        candidate += 2

    # Step 6: Return the list of generated prime numbers.
    return primes


def is_prime(candidate, primes, multiples):
    """
    Determine if a candidate number is prime by checking divisibility against
    previously discovered primes and their multiples.

    Args:
        candidate (int): The number being tested for primality.
        primes (list): The list of discovered prime numbers.
        multiples (list): The list of multiples of discovered primes.

    Returns:
        bool: True if the candidate is prime, False otherwise.

    Algorithm Details:
    ------------------
    - If the candidate equals the square of the largest discovered prime,
      it is marked as composite and added to the list of multiples.
    - Otherwise, check divisibility using previously tracked multiples. If any
      multiple matches the candidate, it is composite (not prime).
    - If no divisors are found, the candidate is considered prime.
    """
    # Retrieve the largest discovered prime and calculate its square.
    largest_prime = primes[-1]
    square_of_largest = largest_prime * largest_prime

    # If the candidate equals the square of the largest prime,
    # add it to multiples and mark it as composite.
    if candidate == square_of_largest:
        multiples.append(candidate)
        return False

    # Check divisibility using previously tracked multiples.
    for i in range(1, len(multiples)):
        # Update multiples until they exceed or match the candidate.
        while multiples[i] < candidate:
            multiples[i] += 2 * primes[i]

        # If any multiple matches the candidate, it is not prime.
        if multiples[i] == candidate:
            return False

    # If no divisors are found, return True (the candidate is prime).
    return True


# Example Usage
# =============
if __name__ == "__main__":
    # Let's generate and print the first 10 prime numbers:
   
    num_primes = 10
   
    print(f"The first {num_primes} prime numbers are:")
   
    # Call `generate_primes` to compute them and print them in a readable format.
   
    print(generate_primes(num_primes))



Date Sujet#  Auteur
29 Sep 24 * Re: The joy of FORTRAN1568186282@ud0s4.net
29 Sep 24 `* Re: The joy of FORTRAN1567Lawrence D'Oliveiro
29 Sep 24  +* Re: The joy of FORTRAN1505rbowman
30 Sep 24  i+- Re: The joy of FORTRAN1Lawrence D'Oliveiro
30 Sep 24  i+* Re: The joy of FORTRAN685186282@ud0s4.net
30 Sep 24  ii`* Re: The joy of FORTRAN684rbowman
30 Sep 24  ii +* Re: The joy of FORTRAN2Lawrence D'Oliveiro
30 Sep 24  ii i`- Re: The joy of FORTRAN1rbowman
30 Sep 24  ii +* Re: The joy of FORTRAN574The Natural Philosopher
30 Sep 24  ii i+* Re: The joy of FORTRAN571Peter Flass
1 Oct 24  ii ii`* Re: The joy of FORTRAN570Stefan Ram
2 Oct 24  ii ii +* Re: The joy of FORTRAN2186282@ud0s4.net
3 Oct 24  ii ii i`- Re: The joy of FORTRAN1moi
25 Feb 25  ii ii `* Re: The joy of FORTRAN567vjp2.at
25 Feb 25  ii ii  +* Re: The joy of FORTRAN378Lawrence D'Oliveiro
25 Feb 25  ii ii  i`* Re: The joy of FORTRAN377Peter Flass
25 Feb 25  ii ii  i +* Re: The joy of FORTRAN369John Ames
25 Feb 25  ii ii  i i+* Re: The joy of FORTRAN236John Ames
25 Feb 25  ii ii  i ii+* Re: The joy of FORTRAN225Peter Flass
26 Feb 25  ii ii  i iii`* Re: The joy of FORTRAN224Lawrence D'Oliveiro
26 Feb 25  ii ii  i iii +* Re: The joy of FORTRAN219John Ames
26 Feb 25  ii ii  i iii i`* Re: The joy of FORTRAN218Lawrence D'Oliveiro
26 Feb 25  ii ii  i iii i +- Re: The joy of FORTRAN1John Ames
27 Feb 25  ii ii  i iii i `* Re: The joy of FORTRAN216Rich Alderson
27 Feb 25  ii ii  i iii i  +* Re: The joy of FORTRAN2Lawrence D'Oliveiro
28 Feb 25  ii ii  i iii i  i`- Re: The joy of FORTRAN1Rich Alderson
27 Feb 25  ii ii  i iii i  `* Re: The joy of FORTRAN213John Ames
27 Feb 25  ii ii  i iii i   +* Re: the end of 18 bits, The joy of FORTRAN6John Levine
27 Feb 25  ii ii  i iii i   i+- Re: the end of 18 bits, The joy of FORTRAN1Lawrence D'Oliveiro
28 Feb 25  ii ii  i iii i   i+* Re: the end of 18 bits, The joy of FORTRAN2John Levine
28 Feb 25  ii ii  i iii i   ii`- Re: the end of 18 bits, The joy of FORTRAN1John Levine
28 Feb 25  ii ii  i iii i   i`* Re: the end of 18 bits, The joy of FORTRAN2Chris Ahlstrom
1 Mar 25  ii ii  i iii i   i `- Re: the end of 18 bits, The joy of FORTRAN1c186282
7 Mar 25  ii ii  i iii i   `* Re: The joy of FORTRAN206Alfred Falk
7 Mar 25  ii ii  i iii i    `* Re: The joy of FORTRAN205Dan Cross
7 Mar 25  ii ii  i iii i     +- Re: The joy of FORTRAN1Lynn Wheeler
7 Mar 25  ii ii  i iii i     `* Re: The joy of FORTRAN203rbowman
8 Mar 25  ii ii  i iii i      +* Re: The joy of FORTRAN3Dan Cross
8 Mar 25  ii ii  i iii i      i`* Re: The joy of FORTRAN2ted@loft.tnolan.com (Ted Nolan
8 Mar 25  ii ii  i iii i      i `- Re: The joy of FORTRAN1rbowman
8 Mar 25  ii ii  i iii i      `* Re: The joy of FORTRAN199c186282
8 Mar 25  ii ii  i iii i       `* Re: The joy of FORTRAN198rbowman
8 Mar 25  ii ii  i iii i        +* Re: The joy of FORTRAN75rbowman
8 Mar 25  ii ii  i iii i        i`* Re: The joy of FORTRAN74The Natural Philosopher
9 Mar 25  ii ii  i iii i        i `* Re: The joy of FORTRAN73c186282
9 Mar 25  ii ii  i iii i        i  `* Re: The joy of FORTRAN72rbowman
9 Mar 25  ii ii  i iii i        i   +* Re: The joy of FORTRAN67c186282
9 Mar 25  ii ii  i iii i        i   i+* Re: The joy of FORTRAN16rbowman
10 Mar 25  ii ii  i iii i        i   ii`* Re: The joy of FORTRAN15c186282
10 Mar 25  ii ii  i iii i        i   ii `* Re: The joy of FORTRAN14rbowman
10 Mar 25  ii ii  i iii i        i   ii  +* Re: The joy of FORTRAN7Sn!pe
10 Mar 25  ii ii  i iii i        i   ii  i+* Re: The joy of FORTRAN5rbowman
10 Mar 25  ii ii  i iii i        i   ii  ii+* Re: The joy of FORTRAN3Sn!pe
10 Mar 25  ii ii  i iii i        i   ii  iii`* Re: The joy of FORTRAN2rbowman
12 Mar 25  ii ii  i iii i        i   ii  iii `- Re: The joy of FORTRAN1c186282
12 Mar 25  ii ii  i iii i        i   ii  ii`- Re: The joy of FORTRAN1c186282
12 Mar 25  ii ii  i iii i        i   ii  i`- Re: The joy of FORTRAN1c186282
11 Mar 25  ii ii  i iii i        i   ii  `* Re: The joy of FORTRAN6c186282
11 Mar 25  ii ii  i iii i        i   ii   +- Re: The joy of FORTRAN1The Natural Philosopher
11 Mar 25  ii ii  i iii i        i   ii   `* Re: The joy of FORTRAN4rbowman
13 Mar 25  ii ii  i iii i        i   ii    `* Re: The joy of FORTRAN3c186282
13 Mar 25  ii ii  i iii i        i   ii     `* Re: The joy of FORTRAN2rbowman
13 Mar 25  ii ii  i iii i        i   ii      `- Re: The joy of FORTRAN1c186282
10 Mar 25  ii ii  i iii i        i   i`* Re: The joy of FORTRAN50Rich Alderson
10 Mar 25  ii ii  i iii i        i   i +* Re: The joy of FORTRAN10c186282
10 Mar 25  ii ii  i iii i        i   i i+* Re: The joy of FORTRAN8rbowman
10 Mar 25  ii ii  i iii i        i   i ii+- Re: The joy of FORTRAN1rbowman
12 Mar 25  ii ii  i iii i        i   i ii`* Re: The joy of FORTRAN6c186282
12 Mar 25  ii ii  i iii i        i   i ii +* Re: The joy of FORTRAN2John Ames
12 Mar 25  ii ii  i iii i        i   i ii i`- Re: The joy of FORTRAN1rbowman
12 Mar 25  ii ii  i iii i        i   i ii `* Re: The joy of FORTRAN3rbowman
12 Mar 25  ii ii  i iii i        i   i ii  `* Re: The joy of FORTRAN2Dan Cross
13 Mar 25  ii ii  i iii i        i   i ii   `- Re: The joy of FORTRAN1c186282
10 Mar 25  ii ii  i iii i        i   i i`- Re: The joy of FORTRAN1ted@loft.tnolan.com (Ted Nolan
10 Mar 25  ii ii  i iii i        i   i +* Re: The joy of FORTRAN33rbowman
10 Mar 25  ii ii  i iii i        i   i i`* Re: The joy of FORTRAN32The Natural Philosopher
10 Mar 25  ii ii  i iii i        i   i i +* Re: The joy of FORTRAN16rbowman
11 Mar 25  ii ii  i iii i        i   i i i+* Re: The joy of FORTRAN13The Natural Philosopher
11 Mar 25  ii ii  i iii i        i   i i ii+* Re: The joy of FORTRAN3rbowman
11 Mar 25  ii ii  i iii i        i   i i iii`* Re: The joy of FORTRAN2The Natural Philosopher
11 Mar 25  ii ii  i iii i        i   i i iii `- Re: The joy of FORTRAN1David LaRue
11 Mar 25  ii ii  i iii i        i   i i ii+- Re: The joy of FORTRAN1The Natural Philosopher
11 Mar 25  ii ii  i iii i        i   i i ii+- Re: The joy of FORTRAN1Lawrence D'Oliveiro
11 Mar 25  ii ii  i iii i        i   i i ii+* Re: The joy of FORTRAN4rbowman
12 Mar 25  ii ii  i iii i        i   i i iii+- Re: The joy of FORTRAN1c186282
12 Mar 25  ii ii  i iii i        i   i i iii`* Re: The joy of FORTRAN2The Natural Philosopher
12 Mar 25  ii ii  i iii i        i   i i iii `- Re: The joy of FORTRAN1The Natural Philosopher
13 Mar 25  ii ii  i iii i        i   i i ii`* Re: The joy of FORTRAN3c186282
13 Mar 25  ii ii  i iii i        i   i i ii `* Re: The joy of FORTRAN2The Natural Philosopher
14 Mar 25  ii ii  i iii i        i   i i ii  `- Re: The joy of FORTRAN1c186282
12 Mar 25  ii ii  i iii i        i   i i i`* Re: The joy of FORTRAN2c186282
12 Mar 25  ii ii  i iii i        i   i i i `- Re: The joy of FORTRAN1The Natural Philosopher
12 Mar 25  ii ii  i iii i        i   i i `* Re: The joy of FORTRAN15c186282
12 Mar 25  ii ii  i iii i        i   i i  `* Re: The joy of FORTRAN14The Natural Philosopher
12 Mar 25  ii ii  i iii i        i   i i   +- Re: The joy of FORTRAN1The Natural Philosopher
13 Mar 25  ii ii  i iii i        i   i i   `* Re: The joy of FORTRAN12rbowman
13 Mar 25  ii ii  i iii i        i   i i    +- Re: The joy of FORTRAN1rbowman
13 Mar 25  ii ii  i iii i        i   i i    +- Re: The joy of FORTRAN1rbowman
14 Mar 25  ii ii  i iii i        i   i i    +* Re: The joy of FORTRAN6Don_from_AZ
14 Mar 25  ii ii  i iii i        i   i i    i+* Re: The joy of FORTRAN2ted@loft.tnolan.com (Ted Nolan
14 Mar 25  ii ii  i iii i        i   i i    ii`- Re: The joy of FORTRAN1c186282
14 Mar 25  ii ii  i iii i        i   i i    i+* Re: The joy of FORTRAN2Lawrence D'Oliveiro
14 Mar 25  ii ii  i iii i        i   i i    i`- Re: The joy of FORTRAN1c186282
15 Mar 25  ii ii  i iii i        i   i i    `* Re: The joy of FORTRAN3Peter Flass
10 Mar 25  ii ii  i iii i        i   i `* Re: The joy of FORTRAN6Don_from_AZ
9 Mar 25  ii ii  i iii i        i   `* Re: The joy of FORTRAN4Kerr-Mudd, John
8 Mar 25  ii ii  i iii i        +* Re: The joy of FORTRAN27Dan Espen
9 Mar 25  ii ii  i iii i        +* Re: The joy of FORTRAN9c186282
9 Mar 25  ii ii  i iii i        `* Re: The joy of FORTRAN86c186282
26 Feb 25  ii ii  i iii +* Re: The joy of FORTRAN3rbowman
27 Feb 25  ii ii  i iii `- Re: The joy of FORTRAN1Rich Alderson
25 Feb 25  ii ii  i ii+- Re: The joy of FORTRAN1Peter Flass
26 Feb 25  ii ii  i ii+* Re: The joy of FORTRAN7Rich
26 Feb 25  ii ii  i ii`* Re: The joy of old small computers, which sort of ran FORTRAN2John Levine
25 Feb 25  ii ii  i i+* Re: The joy of FORTRAN101Peter Flass
26 Feb 25  ii ii  i i+* Re: The joy of FORTRAN27John Levine
26 Feb 25  ii ii  i i+* Re: The joy of FORTRAN3c186282
28 Feb 25  ii ii  i i`- Re: The joy of FORTRAN1c186282
25 Feb 25  ii ii  i +* Re: The joy of FORTRAN4Bob Eager
25 Feb 25  ii ii  i `* Re: The joy of FORTRAN3Lawrence D'Oliveiro
26 Feb 25  ii ii  `* Re: The joy of FORTRAN188c186282
1 Oct 24  ii i`* Re: The joy of FORTRAN2rbowman
30 Sep 24  ii +* OT ; Re: The joy of FORTRAN100Bobbie Sellers
30 Sep 24  ii `* Re: The joy of FORTRAN7Peter Flass
30 Sep 24  i`* Re: The joy of FORTRAN818The Natural Philosopher
30 Sep 24  `* Re: The joy of FORTRAN61186282@ud0s4.net

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal