Assignment problem 1
Part 2a: Prime Number Finder
(Note part 2a, 2b and 2c are the
same parts of the whole problem 2
ut they should be done in separate
files)
Write a program that prompts the user to enter in a positive
number. Only accept positive numbers - if the user supplies a
negative number or zero you should re-prompt them. You can
assume an integer will be entered here.
Next, determine if the given number is a prime number. A prime
number is a number that has no positive divisors other than 1 and
itself. For example, 5 is prime because the only numbers that
evenly divide into 5 are 1 and 5. 6, however, is not prime because
1, 2, 3 and 6 are all divisors of 6.
Here's a sample running of the program:
Enter a positive number to test: 5
2 is NOT a divisor of XXXXXXXXXXcontinuing
3 is NOT a divisor of XXXXXXXXXXcontinuing
4 is NOT a divisor of XXXXXXXXXXcontinuing
5 is a prime number!
And here's another running:
Enter a positive number to test: 9
2 is NOT a divisor of XXXXXXXXXXcontinuing
3 is a divisor of XXXXXXXXXXstopping
9 is not a prime number.
Some notes on your program:
• 1 is technically not a prime number.
• Once you find a number that evenly divides into your test
number you do not need to continue testing additional
numbers - the number cannot be prime.
Part 2b: Find all Prime Numbers
etween 1 and 1000
Next, make a copy of Part A and update it so that the program now
finds all prime numbers between 1 and 1000. Here's a sample
unning of your program:
1 is technically not a prime number.
2 is a prime number!
3 is a prime number!
5 is a prime number!
7 is a prime number!
11 is a prime number!
... cut ...
977 is a prime number!
983 is a prime number!
991 is a prime number!
997 is a prime number!
Part 2c: Custom Number Range
Make a copy of Part B and update it so that the user can choose to
examine a specific range of numbers for prime numbers. Here's a
sample running of your program:
Start number: 5
End number: -5
Start and end must be positive
Start number: 5
End number: 3
End number must be greater than start number
Start number: 5
End number: 23
5
7
11
13
17
19
23
Some notes on your program:
• You need to ensure that the start and end numbers are both
positive. You can assume an integer will be entered for both
inputs.
• You also need to ensure that the start number is less than the
end number.
Assignment problem 2
Part 3: Modern Art Monte Carlo
Simulation
You've decided to visit the Metropolitan Museum of Art with some
friends and notice an interesting painting on the wall:
One of your friends wonders out loud how probable it would to hit
a colored section of the painting if they were to close their eyes
and throw a dart at random at the canvas. You tell your friend that
this is a very bad idea, but because you are in an Introduction to
Computer Programming class you know that you can use your
newly acquired Python skills to solve this problem!
For this program you will be writing some code that will simulate
the throwing of a random dart at this modern art painting. To do
this you can repeatedly generate a random coordinate within the
painting area and test to see which shape (if any) the dart falls into.
Note that the playing field is 800 units wide and 500 units high, so
you will be generating random coordinates within this range as part
of your simulation.
When you are finished you should calculate the % chance of a dart
hitting a particular shape. Refer to the schematic below for the
exact measurements of the painting. Hint: Start off by simulating
just one dart toss and use the coordinate plane to determine if the
dart has fallen into one of the colored regions. Start off with the red
ectangle since it will be the simplest shape to work with -- how
can you write a statement to determine if a point falls within a
ectangle? From there, expand this logic and apply it to the other
shapes. Hint: you may need the distance formula again to
determine if a point falls within a circular region! Once you are
confident that you have designed an effective algorithm you can
scale up and place your code inside a loop.
Here’s a a few sample runnings of the program with user input
underlined:
Number of throws: 10000
Total time elapsed: 0.02 seconds
Red XXXXXXXXXX, XXXXXXXXXX%)
Green XXXXXXXXXX65%)
Blue XXXXXXXXXX75%)
Grey XXXXXXXXXX32%)
Yellow XXXXXXXXXX%)
Misses XXXXXXXXXX, XXXXXXXXXX%)
Number of throws: -50
Invalid, try again.
Number of throws: 100000
Total time elapsed: 0.21 seconds
Red XXXXXXXXXX, XXXXXXXXXX%)
Green XXXXXXXXXX, XXXXXXXXXX%)
Blue XXXXXXXXXX, XXXXXXXXXX%)
Grey XXXXXXXXXX, XXXXXXXXXX%)
Yellow XXXXXXXXXX, XXXXXXXXXX%)
Misses XXXXXXXXXX, XXXXXXXXXX%))
Number of throws: XXXXXXXXXX
Total time elapsed: 9.92 seconds
Red XXXXXXXXXX, XXXXXXXXXX%)
Green XXXXXXXXXX, XXXXXXXXXX%)
Blue XXXXXXXXXX, XXXXXXXXXX%)
Grey XXXXXXXXXX, XXXXXXXXXX%)
Yellow XXXXXXXXXX, XXXXXXXXXX%)
Misses 3,146, XXXXXXXXXX%)
Some hints:
• You can always assume the user will enter an integer when
prompted, but you will need to validate it so that it falls within
the desired range (1 or larger)
• Start off by generating a single random point within the
ounds of the painting (an x value between 0 and 800 and a y
value between 0 and 500). The coordinate system shown here
is one that is standard in the world of computer graphics --
notice that the origin point is at the top left side of the screen,
and that the y axis only contains positive values.
• You can generate a random floating point number within a
given range by using the random.uniform() function. This
function takes two arguments - a low boundary and a high
oundary - and generates a floating point number within this
ange (edges inclusive)
• Next, write a series of tests to see if the number you just
picked falls within one of the shapes shown. If so, you should
update an accumulator to go count this as a "hit" to that
particular region.
• The distance formula may be helpful in determining if a point
falls within a circle (hint: if the distance from a point to the
center of a circle is less than the radius of that circle then the
point can be considered inside of that circle.
• The green circle intersects with the blue circle. Their
intersection forms a grey region.
• Once this works for a single point you will need to design a
loop to iterate a large number of times and repeat this
process. We sometimes call this a 'Monte Carlo' simulation,
which is designed to approximate a solution to a problem
through the use of random numbers.
• Because of this you will see different numbers when you run
your program. The percentages that get generates should
oughly match the percentages shown here.
• The more "throws" you compute the closer you will get to the
actual solution.
• You can ask Python to generate the cu
ent time by calling the
time.time() function. You will need to import the time module in
order to call this function. This function returns the cu
ent
time based on your computer's internal clock as a single
floating point number. Every time you call this number it will
get bigger, indicating the cu
ent time as measured in
'seconds' since the Unix Epoch. Hint: to compute the total
execution time of your program you may need to capture the
time before you begin your simulation and after your
simulation finishes.
• Your "execution time" value will likely be different than the
ones given here in the sample output. This is due to the fact
that every computer will run at a slightly different rate based
on processor speed, available memory, etc.
• You will need to format your output to match the sample
output above (i.e. all of your values should line up)
https:
en.wikipedia.org/wiki/Unix_time
Extra Credit: Modern Art
Visualization with Turtle Graphics
For this program you will be using Turtle Graphics to display a
graphical representation of your modern art painting "throws".
Here's a visual of what this could look like:
Turtle Graphics rendering of 10,000 throws
Turtle Graphics rendering of 100,000 throws
Here are some hints on how to get started:
• Begin by asking the user if they want to draw the result of the
program. This input should be validated and should be case
insensitive (i.e. "YES", "yes" and any other case variation of
the word "yes" should both turn on Turtle Graphics mode.
• First, set up a canvas that will be big enough to represent your
throws. For example, the size of the painting board is 800px
y 500px - perhaps you could set up a canvas that mi
ored
those proportions
• Next,