Programming Assignment - 12

Programming Assignment - Week 12


Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity.

Programming Assignment 1

Question:

Write a recursive function named collatz that accepts a positive integer n as an argument, where 1 < n ≤ 32,000, and returns the number of times the Collatz function has to be applied repeatedly in order to first reach 1.

The Collatz function is defined as follows:

  • If n is even, divide n by 2.
  • If n is odd, multiply n by 3 and add 1.

The function should continue applying this process until n becomes 1, and return the number of steps taken.

Input Format:

  • A single integer n where 1 < n ≤ 32,000.

Output Format:

  • A single integer representing the number of steps required for n to reach 1.

Example:

Input: 12

Output: 9
  

Code:

def collatz(n):
    if n == 1:
        return 0
    elif n % 2 == 0:
        return 1 + collatz(n // 2)
    else:
        return 1 + collatz(n * 3 + 1)

n = int(input())
print(collatz(n), end="")

Programming Assignment 2

Question:

There are n people who start in a happy state. The following rules determine how people move between states after each iteration:

  • A person in the happy state has a 70% chance of moving to a sad state, and a 30% chance of staying in the happy state.
  • A person in the sad state has a 50% chance of moving to a happy state, and a 50% chance of staying in the sad state.

Write a program that computes the number of people in the happy and sad states after 3 iterations.

Input Format:

  • A single integer n representing the number of people who start in the happy state.

Output Format:

  • Two integers separated by a space, where the first integer is the number of people in the happy state and the second is the number of people in the sad state after the 3rd iteration.

Example:

Input: 1000

Output: 412 588
  

Explanation:

State 0: 1000 0

State 1: 300 700

State 2: 440 560

State 3: 412 588

Code:

def simulate_people_states(n):  
    happy = n
    sad = 0
    for _ in range(3):        
        new_happy = 0.3 * happy + 0.5 * sad
        new_sad = 0.7 * happy + 0.5 * sad        
        happy = new_happy
        sad = new_sad   
    happy = int(round(happy))
    sad = int(round(sad))

    return happy, sad

n = int(input())
happy, sad = simulate_people_states(n)
print(happy, sad,end="")

Programming Assignment 3

Problem Statement:

Write a program that accepts a positive integer n and prints the sum of all prime numbers in the range [1, n], including both endpoints. If there are no prime numbers in the given range, the program should print 0.

Input Format:

  • A single integer n, representing the upper limit of the range.

Output Format:

  • A single integer representing the sum of all prime numbers in the range [1, n]. If no prime numbers are found, output 0.

Example:

Input: 10

Output: 17
  

Explanation:

The prime numbers between 1 and 10 are 2, 3, 5, and 7. Their sum is 2 + 3 + 5 + 7 = 17.

Code:

def sum_of_primes(n): 
    if n < 2:
        return 0    
    primes = [True] * (n + 1)
    primes[0] = primes[1] = False  # 0 and 1 are not primes   
    for i in range(2, int(n**0.5) + 1):
        if primes[i]:
            for j in range(i * i, n + 1, i):
                primes[j] = False

    prime_sum = sum(i for i in range(2, n + 1) if primes[i])

    return prime_sum

n = int(input())
print(sum_of_primes(n),end="")

Comments

Popular posts from this blog

The Joy of Computing using python -Week 1