+1 (315) 557-6473 

Arithmetical Assignment Solution using Python


Question 1:

A standard science experiment is to drop a ball and see how high it bounces. The “bounciness” of the ball is a number between 0.0 and 0.99 (called the bounce coefficient) determines how high the ball will bounce back after it is dropped. For example, if a ball is dropped from a height of 10 feet and has a bounce coefficient of 0.6, then it will bounce to a height of 10*0.6 = 6 ft. The total distance traveled after one bounce will be 22 ft (10 + 6 + 6). The ball keeps bouncing successively to lower and lower heights until it no bounces to such a small height (in inches) that it is considered not bouncing (stop threshold). Write a program that given the height of the initial drop (in ft), the bounce coefficient, and the stop threshold (in inches), prints a table of the height of the ball after each bounce and computes the total distance traveled by the ball. To write a Python homework you should check to see if the input is within bounds and makes experimental sense – or else, the program terminates. See the example output below.

Example Output 1

Example Output 2

Example Output 3

Make sure you test the output using a calculator.

Question 2:

In cyber security, prime numbers are very important for studying cryptography. To get an idea of how long it takes to generate large prime numbers, you have to write a python program that generates successive prime numbers. The program asks the user to enter the base number beyond which the prime number is generated. The base number could be long (big), so to make it easy for the user to enter the number, the power of 2 from which the base number should begin is entered. The program also asks for the number of prime numbers to be generated. It then prints out the table of prime numbers as shown below.

To determine if a number is prime, the following steps are used:

For any number X greater than or equal to 2, determine the integer part of the square root of X (say the integer part of the square root is Y). Then use a loop in which a variable Z is incremented by one from 2 to Y, the square root of the number. Within the loop, check to see if X mod Z is zero. If it is, exit the loop and report X is not prime. If the loop finishes successfully for all values from 2 to Y (without exiting in the middle), then the number X is a prime.

The program should check for valid input. You can set the range of the power of two based on the power of the computer you are using.

Example interaction is given below.

Example Output 4

Example Output 5

Question 3:

The Lucky Sevens program as given and implemented in class assumes that a customer will play until they have no more money left in the pot to wager, and the casino is always the eventual winner. However, in real life, that does not happen. Customers continue to play only when they feel like they are winning, and if not, they quit after a few tries. To ensure that the casino overall wins (but also gives customers a fair chance of winning), they contacted a Luck Seven gaming psychologist, who gave these recommendations:

  1. If a customer is winning more than 10% of the time and has more money than he/she has started, then there is a 90% chance that the customer will leave with the winnings.
  2. If a customer is losing, they will quit when they have between 40% and 60% of their total money (Pot) that they had initially decided to wager. They will realize that the “table” is unlucky and move on.

You have been hired by the casino to write a python that incorporates the game of Lucky Sevens as given in the text project, but with the above two recommendations of the psychologist. The program prints simulation of the results of running the game a few times (ask user), the amount a customer can wager per game (between $1 and $ 10), and the amount the casino gives as winning for each game (between $1 and $100, but always greater than the wager per game). The simulation prints the following in a tabulated form:

Pot Value (initial total amount customer is planning to wager). For the simulation assume a random integer number between 1 and 100

NumRolls: Number of rolls the customer played before quitting Winning Rolls: The number of winning rolls the customer had Losing Rolls: The number of rolls the customer did not win

Times Pot Exceeded: The number of times the pot exceeded the initial pot

Max Winnings: The maximum amount a customer was winning at any point in the game

Casino Winnings: The amount the casino won from the number of rolls from the customer. A negative amount indicates that the casino lost that much money – which was won by the customer.

At the end of the table, the program prints the total number of amounts the customer won and the casino won after all the customers played that game.

Please make sure that the arithmetic is working out correctly by showing an arithmetic calculation.

Solution:

1.

if __name__ == '__main__': height = int(input('Enter the height in feet from which the ball is dropped (integer 1 to 1000): ')) if height < 1 or height > 1000: print('Incorrect height entered. Program has terminated') else: bouncing = float(input('Enter the bounce coefficient as a number between 0.0 and 0.99 (float rounded to 2 decimal places): ')) if bouncing < 0.00 or bouncing > 0.99: print('Incorrect coefficient entered. Program has terminated') else: bouncing = round(bouncing, 2) not_bounce_height = float(input('Enter the height in inches after which the ball is considered to not bounce (ex: 0.01, rounded to 3 decimal places): ')) if not_bounce_height < 0.0 or not_bounce_height >= 12: print('Incorrect stop threshold. Program has terminated') else: not_bounce_height = round(not_bounce_height, 3) / 12 number = 0 distance = height print('{:<15s}'.format('Bounce number'),'{:<15s}'.format('Current Height (ft)')) while height > not_bounce_height: height *= bouncing number += 1 distance += 2 * height print('{:>8d}'.format(number), '{:>6s}'.format(' '), '{:>12.4f}'.format(height)) print() print('Total Bounces:', number, ' ', 'Total distance :', '{:.2f}'.format(distance)) print('The program Ball bounce has ended')

2.

import math def is_prime(i): j = 2 while j * j <= i: if i % j == 0: return False j += 1 return True def find_next_prime(i): curr = i+1 while not is_prime(curr): curr += 1 return curr if __name__ == '__main__': print('This program allows you to print prime numbers.') print('You have to enter the starting power of 2 beyond which you want your prime number.') print('Then enter the number of prime numbers you want.') power = int(input('Enter the starting power of 2 beyond which you want your prime numbers to begin (Integer between 1 and 60): ')) if power < 1 or power > 60: print('Incorrect power of 2 entered. Program has terminated') else: n = int(input('Enter the number of prime numbers you want (Integer between 1 and 1000): ')) if n < 1 or n > 1000: print('Incorrect number of prime numbers entered. Program has terminated') else: start = int(math.pow(2, power)) curr = start for i in range(1, n+1): curr = find_next_prime(curr) print('{:4d}'.format(i) + ':Prime Number greater than', start, 'is', curr)

3.

import random def play_roll(): return (random.randint(1, 6) + random.randint(1, 6)) == 7 if __name__ == '__main__': customers = int(input('Enter the number of customers who play Lucky Seven (1 -100000): ')) if customers < 1 or customers > 100000: print('Incorrect number of customers entered. Program has terminated') else: per_game = int(input('Enter the amount customer ($) wagers per game (1-10): ')) if per_game < 1 or per_game > 10: print('Incorrect amount entered. Program has terminated') else: wins_per_game = int(input('Enter the amount customer ($) wins per game (1-100), greater then wager: ')) if wins_per_game < 1 or wins_per_game > 10 or wins_per_game <= per_game: print('Incorrect amount entered. Program has terminated') else: print('Pot Value', 'NumRolls', 'Winning Rolls', 'Losing Rolls', 'Times Pot Exceeded', 'Max Winnings$', 'Casino Winnings$') total_casino_win = 0 total_customer_win = 0 for i in range(customers): pot = random.randint(1, 100) num_rolls = 0 winning_rolls = 0 losing_rolls = 0 pot_exceeded = 0 max_winning = 0 amount = pot while True: result = play_roll() num_rolls += 1 if result: winning_rolls += 1 if amount <= pot and amount + wins_per_game - per_game > pot: pot_exceeded += 1 amount += wins_per_game - per_game max_winning = max(max_winning, amount - pot) if winning_rolls > num_rolls * 0.1 and amount > pot and random.random() < 0.9: break else: losing_rolls += 1 amount -= per_game if 0.4 * pot <= amount <= 0.6 * pot: break if amount < per_game: print('NOT ENOUGH MONEY') break casino_winnings = num_rolls*per_game - winning_rolls*wins_per_game if casino_winnings >= 0: total_casino_win += casino_winnings else: total_customer_win -= casino_winnings print('{:^10d}'.format(pot), '{:^8d}'.format(num_rolls), '{:^13d}'.format(winning_rolls), '{:^12d}'.format(losing_rolls), '{:^18d}'.format(pot_exceeded), '{:^13d}'.format(max_winning), '{:^16d}'.format(casino_winnings)) print('Total Casino Winnings $', total_casino_win) print('Total Customer Winnings $', total_customer_win)