deferatosthenes_sieve(n): primes = [] sieve = [True] * (n +1)for p inrange(2, n +1):if sieve[p]: primes.append(p)for i inrange(p * p, n +1, p): sieve[i]=Falsereturn primes
GitHub Copilot は以下のようなメソッドの解説のコメントを書くことができます。
# Write the description of the method here <- [Actual Prompt]# Input: n - the number of primes to return# Output: a list of the first n primes# Example: eratosthenes_sieve(5) -> [2, 3, 5, 7, 11]# Note: this is a very inefficient way to find primes, but it is easy to understanddeferatosthenes_sieve(n): primes = [] sieve = [True] * (n +1)for p inrange(2, n +1):if sieve[p]: primes.append(p)for i inrange(p * p, n +1, p): sieve[i]=Falsereturn primes
deferatosthenes_sieve(n): primes = [] sieve = [True] * (n +1)# Description of the algorithm <- [Actual Prompt]# 1. Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).# 2. Initially, let p equal 2, the first prime number.# 3. Starting from p, enumerate its multiples by counting to n in increments of p, and mark them in the list# (these will be 2p, 3p, 4p, ...; the p itself should not be marked).# 4. Find the first number greater than p in the list that is not marked. If there was no such number, stop.# Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.for p inrange(2, n +1):if sieve[p]: primes.append(p)for i inrange(p * p, n +1, p): sieve[i]=Falsereturn primes
ステップの表示
GitHub Next の Code Brushes にある LIST STEPS 機能を使うことで、同様のことが可能です。
defcalculate_sum(numbers):# initialize a variable to track the total total =0# iterate over each number in the listfor number in numbers:# add the number to the total total += number# return the totalreturn total