Practice 4 (Year 2 Sem 1)

Practice 4 is the fourth practice in semester 1 of year 2, and the fourth practice in the total of year 2. It is a practice for loops, and is considered to be not difficult.

Description
No description was provided for this practice.

Unlocked after completion

 * Practice makes perfect! (Score at least 60% for Practice 4 [Chapter 6])


 * Intern of the Year (Score at least 60% for Practice 4 [Chapter 6])


 * Challenge 3: Loops (Complete Practice 4 [Chapter 6])

Question
Use a  loop to calculate the sum of the first 1337 natural numbers. (That is, 1 + 2 + 3 + ... + 1335 + 1336 + 1337)

Store the result inside the  variable already provided for you.

Solution
If you do not know how to do this question, you should seek help immediately.

(If you are still unsure about how to do this question, use the fact that a variable is present to determine loop number in your for loop, also your range must start from 1 and not 0)

Question
Write the code required to sum up consecutive even integers between a and b (inclusive of a and b).

Note that: You are to use a  loop for this question.
 * If a is not an even integer, the sum should start from the next even integer greater than a.
 * If b is not even, the sum should stop at the next even integer smaller than b.

Solution
Test if a is even. If not, you should increment it by 1. Test if b is even. If it is, increment it by 1. After that, you should use a while loop for the rest.

Question
A function  has been created. It takes in one integer as input, and returns  if the input matches a certain integer Z, and   otherwise. It is known that Z is between 1 and 1000 inclusive. For example, if Z = 201,  will return   and other inputs such as ,   and   will return True.

Your job is to use a  loop to determine the value of Z. Starting with z = 1, keep testing   until it returns , at which point we have found Z.

Solution
Use a while loop to test all values between 1 and 1000. This should be an easy task.

Question
You can approximate e by using the following series:



Write the code required to estimate e, given input i.

Algorithms
Use a for loop for this question.

If you want, you can use math.factorial for the factorials, else calculate it by yourself with another for loop.

Question
A perfect number is a positive integer that is equal to the sum of its proper divisors. A proper divisor is a positive integer other than the number itself that divides the number evenly (i.e. no remainder). For example, 6 is the smallest perfect number, because the sum of its proper divisors 1, 2, and 3 is equal to 6. 8 is not a perfect number because 1 + 2 + 4 is not equal to 8.

Given a positive integer n, determine if it is a perfect number. Return  if n is a perfect number,   otherwise.

Hint 1

if a is a divisor of n, then n// a is also a divisor of n.

Hint 2

checking for all integers from 1 to n is too slow, you need to check a smaller range.

Solution
You can use a for i in range(1, sqrt(n)) and if n%i == 0 then add i and n//i to the total. If the total is n then it's a perfect number woohoo

However if you're a nerd you can use the following solution:

Over 2300 years ago Euclid proved that if (2^k)-1 is a prime number, making it a Mersenne Prime, then 2^(k-1) multiplied by ((2^k)-1) is a perfect number. A few hundred years ago Euler proved the converse (that every even perfect number has this form).

Therefore you can implement this algorithm.

If $$2^k-1$$ is prime, $$(2^k-1)\times(2^{k-1})$$ is a perfect number.

History of p4q5
Someone hardcoded p4q5 and mr cs lim came after him by sending him an angry email. The email is stated below.

Angry Email 1
Its very evident you have an issue with integrity, being able to lie about your lack of coding ability.

See the picture below. Such submissions are NOT accepted!

I will not accept such rubbish from you and you jolly well do it properly. If you have other of such nonsensical submissions you better own up before I find out and I will make you go through even worse consequences.

GO AND DO PRACTICE 4 Q5 AGAIN! If you are not sure how to do it you jolly well put your ego aside and ask.

The picture below
It is unclear what the picture below is, as it has only been revealed to a select few.

Events leading up to the second angry email
After redoing the question after the unsubmission, the same person was falsely accused of plagiarism from Stack Overflow, as he used a list generated solution, which had not been taught yet.

Below is the second angry email.

Angry Email 2
Go and redo Practice 4 Qn5 again. I will not accept your solution because your solution is a generated list which is clearly a stackoverflow solution, reflecting that you clearly do not understand how to do the question and decided to just take a solution from the Internet.

I will NOT allow a list solution for this practice. Don’t know how to do, jolly well ask. DO NOT make me chase after you for this.

Events after the second angry email
During the third attempt, Practice 4 Question 5 was completed, without any emails from mr cs lim.

Question
What is the output of the following code snippet? (You may want some paper to work it out.)

Options

 * 2
 * 30
 * 0
 * 15