Question: Question 1. # prime_counter.py: takes an integer N as command-line argument and writes the # number of primes less than or equal to N. import
Question 1.
# prime_counter.py: takes an integer N as command-line argument and writes the # number of primes less than or equal to N.
import stdio import sys
# Get N from command line, as an int. N = ...
# Define primes to store the number of primes <= N. ...
# Iterate over integers 2 to N (inclusive). for i in range(..., ...): # Define a variable j to store the potential divisors of i, and initialize # it to 2. ...
# Test if i is prime. Repeat as long as j is <= square root of i. while ...: # If i is divisible by j, it is not a prime so exit this inner loop. if ...: ...
# Increment j. ...
# If j is > square root of i, then we got here by exhausting the while # loop, and i is therefore a prime. So increment primes by one. if ...: ...
# Write primes. ...
do not use print, use stdio.writeln instead.
Question 2.
# root.py: takes a float c and an integer k as command-line arguments and # writes the kth root of c to 5 decimal places of accuracy.
import stdio import sys
# Get c from command line, as a float. c = ...
# Get k from command line, as an int. k = ...
# Define epsilon to be 0.00001 (for 5 decimal places of accuracy). epsilon = ...
# Define a variable t (kth root of c) and initialize it to the value of c. t = ...
# Repeat until t is accurate enough. while ...: # Improve t. t = ...
# Write t. ...
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
