homework 4
In this exercise, use the following variables : i,lo, hi, and result. Assume that lo and hi each are associated with an int and that result refers to 0. Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result. Your code should not change the values associated with lo and hi. Also, just use these variables : i,lo, hi, and result.
"Assume that lo and hi each are associated with an int"- don't need to define them "result refers to 0"- need to define result as result = 0 "adds the integers from lo up through hi (inclusive)"- starts from lo, will add 1 until reaches high, keep associating it with variable "result"- code: result = 0 i = lo while i<= hi : result = result + i i += 1
Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,...]) can be added before exceeding n. Associate this number with the variable k. **HELP**
"Assume the availability of a function is_prime. "- we can use a function known as "is_prime" that is already defined??? "Assume a variable n has been associated with positive integer"- don't need to define n "needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,...]) can be added before exceeding n"- since is_prime is already defined, need to use this function to first prove if a value is prime, and if it is, it can be added to k (but don't add it's actual value, because this problem just wants to know how many prime numbers are in this progression) variables: n, is_prime,x,k code: x=2 k=0 while k<=n: if is_prime(x) == True: k += 1 x +=1 else: x += 1 actual: i = 2 k = 0 sum = 0 while (sum + i <= n) : if (is_prime(i)) : sum += i k += 1 i += 1 ?????
Assume there is a variable , h already associated with a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 19, you would assign 30 to q because the perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16 and 30==1+4+9+16.
"Assume there is a variable , h already associated with a positive integer value"- don't need to define h "compute the sum of the perfect squares whose value is less than h"- sum<h, or actual q<h *actually referring to value of x, not value of sum that has to be smaller than h* code: x=1 q=0 while x<h: q += x**2 x += 1
Assume there is a variable , h already associated with a positive integer value. Write the code necessary to compute the sum of the first h perfect squares, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 4, you would assign 30 to q because the first 4 perfect squares (starting with 1) are: 1, 4, 9, 16 and 30==1+4+9+16.
"Assume there is a variable , h already associated with a positive integer value"- no need to define it "Write the code necessary to compute the sum of the first h perfect squares, starting with 1."- finds first h perfect squares and adds them together variables: h, x, q code: x=1 q=0 while x<=h: q += x**2 x += 1
Given a variable n that is associated with a positive integer and a variable s that is associated with the empty string, write some statements that use a while loop to associate s with a string consisting of exactly n asterisks (*) . (So if n were associated with 6 then s would, in the end, be associated with "******" .
"Given a variable n that is associated with a positive integer and a variable s that is associated with the empty string"- both are already defined variables: n, s,x code: s="" x=0 while x<=n: s += "*" x += 1
Given that n refers to a positive int use a while loop to compute the sum of the cubes of the first n counting numbers, and associate this value with total. Use no variables other than n, k, and total.
"Given that n refers to a positive int"- no need to define n "compute the sum of the cubes of the first n counting numbers"- will keep adding the cubes of n counting numbers code: k=1 total=0 while k<=n: total += k**3 k += 1
Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 with total. Use no variables other than k and total.
"Use the variables k and total"- need to define them if not explicitly stated "computes the sum of the squares of the first 50 counting numbers"- adds up first 50 squares then will assign it to a value called "total" code: k=1 total=0 while k<=50: total += k**2 k += 1
An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6. Given the positive integer distance and the positive integer n, associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance. For example, if distance is 2 and n is 10, then sum would be associated with 25 because 1+3+5+7+9 = 25.
x=1 sum=0 while x<=n: sum += x x += distance