The following function returns a list of all prime numbers up to the figure supplied to it (n). Example:

import math

def prime(n):
    answer = []
    if n < 2:
        return answer
    
    for loop in range(2, n+1):    
        isPrime = True
        upper = int(math.sqrt(loop))+1
        for i in range(2, upper):
            if loop % i == 0:
                isPrime = False
        if(isPrime):
            answer = answer + [loop]

    return answer

>>> prime(20)
[2, 3, 5, 7, 11, 13, 17, 19]
>>>

Using this function, write a program to show that each even number between any two user supplied integers, greater than 3, is the sum of two prime numbers (ensure that for each number you print the prime numbers which sum to it).


For example:

>>> question1()
Enter two numbers (greater than 3 and lowest first)5,30
Demonstrating that even numbers between 5 and 30 are the sum of two prime numbers
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17
22 = 3 + 19
24 = 5 + 19
26 = 3 + 23
28 = 5 + 23

Chapter 9 of your textbook (and your lecture notes) refer to a racquetball simulation program (which is attached for your reference). Make the following modifications to the program:

a) Ensure that to win a game (in addition to scoring 15 points) a player must obtain a 2 point margin from the opponent.

b) Players will now have a defensive probability entered (the odds of winning the serve back).

The probability of winning a point on serve becomes: 0.5 + Probability of winning a serve – Probability of opponent successfully defending. Example:

Player A (serving) Attacking Probability = 0.7
Player B (receiving) Defending Probability = 0.65
Player A’s probability of winning serve = 0.5 + 0.7 – 0.65 = 0.55

c) Print detailed results for each game including the number of points played in total and the final score. Attach the results of at least 4 separate matchups each over at least five games.


Original Racquetball simulation output:

>>>
This program simulates a game of racquetball between two
players called "A" and "B". The abilities of each player is
indicated by a probability (a number between 0 and 1) that
the player wins the point when serving. Player A always
has the first serve.

What is the prob. player A wins a serve? .6
What is the prob. player B wins a serve? .7
How many games to simulate? 20

Games simulated: 20
Wins for A: 2 (10.0%)
Wins for B: 18 (90.0%)
>>>

# rball.py
#   Simulation of a racquetball game. Illustrates use of random
#    numbers and functions to implement top-down design.

from random import random

def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)

def printIntro():
    # Prints an introduction to the program
    print "This program simulates a game of racquetball between two"
    print 'players called "A" and "B".  The abilities of each player is'
    print "indicated by a probability (a number between 0 and 1) that"
    print "the player wins the point when serving. Player A always"
    print "has the first serve.\n"

def getInputs():
    # Returns probA, probB, number of games to simulate
    a = input("What is the prob. player A wins a serve? ")
    b = input("What is the prob. player B wins a serve? ")
    n = input("How many games to simulate? ")
    return a, b, n

def simNGames(n, probA, probB):
    # Simulates n games of racquetball between players whose
    #    abilities are represented by the probability of winning a serve.
    # Returns number of wins for A and B
    winsA = winsB = 0
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA = winsA + 1
        else:
            winsB = winsB + 1
    return winsA, winsB

def simOneGame(probA, probB):
    # Simulates a single game of racquetball between two players whose
    #    abilities are represented by the probability of winning a serve.
    # Returns final scores for A and B
    serving = "A"
    scoreA = 0
    scoreB = 0
    while not gameOver(scoreA, scoreB):
        if serving == "A":
            if random() < probA:
                scoreA = scoreA + 1
            else:
                serving = "B"
        else:
            if random() < probB:
                scoreB = scoreB + 1
            else:
                serving = "A"
    return scoreA, scoreB

def gameOver(a,b):
    # a and b are scores for players in a racquetball game
    # Returns true if game is over, false otherwise
    return a == 15 or b == 15

def printSummary(winsA, winsB):
    # Prints a summary of wins for each player.
    n = winsA + winsB
    print "\nGames simulated:", n
    print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
    print "Wins for B: %d (%0.1f%%)" % (winsB, float(winsB)/n*100)

if __name__ == "__main__": main()

How about you show us what you've tried and we'll help you. That's how it's supposed to work, we're not here to do your homework.

And when you're posting code please use code tags:
[code=python] # Code in here

[/code]
When you do that you can make code that looks like this:

import os
#
if os.path.exists('/home/user/friends'):
    print 'Nice!  Friends!!'

See how nice that looks?

commented: thanks for the code tag info +11

Yeap I knw hw beautfl it looks bt thng is I dnt knw much abt progmng...ie y I askd help...

Yeap I knw hw beautfl it looks bt thng is I dnt knw much abt progmng...ie y I askd help...

Pretty goofy English there!

At first you need to help yourself by studying the manual a little.

Yeap I knw hw beautfl it looks bt thng is I dnt knw much abt progmng...ie y I askd help...

And the way your going now, you never will know much about it.
You only learn by doing it.
(And the "chat-speak" only reinforces the impression of lazy and ignorant.)

Oh that's chat speak? I thought maybe he wasn't a native english speaker and I was about to chastise people for teasing him about it. Nevermind.

Is that chat speak or did he get disemvoweled ?

commented: :) +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.