Another beautiful day in this miserable world

Sorry guys :(

Sorry for the lack of posts. With class starting, I haven’t had much time other than work and school. My new class focuses on Java, so my next few posts will most likely be on those. I hope to get back to Python soon, Java sucks 🙂

12. Write a program to find the sum of the cubes of the first n natural numbers where the value of n is provided by the user


def main():
    print("This program finds the sum of the cubes of the first ")
    print("'n' natural numbers starting with 0")
    n = eval(input("Enter any natural number "))
    add = 0
    for i in range (n):
        add = add + i**3
    print(add)
main()

11. Write a program to find the sum of the first n natural numbers where the value of n is provided by the user.

def main():
    print("This program finds the sum of the first 'n' natural numbers")
    print("starting with 0")
    n = eval(input("Enter any natural number "))
    add = 0
    for i in range (n):
        add = add + i
    print(add)
main()

10. Write a program to determine the length of a ladder required to reach a given height when leaned against a house. Prompt for an angle in degrees and convert it to radians.


from math import *
def main():
    height = eval(input("Please enter the height of the ladder "))
    deg = eval(input("Please enter the angle of the ladder in degrees "))
    rad = (pi/180) * deg
    leng = height/(sin(rad))
    print("The length of the ladder must be ", leng)
main()

9. Write a program to calculate the area of a triangle given the length of its three sides a,b, and c.

We will have to change the formula given in the book a little bit due to Python not recognizing a number outside of a parenthesis as multiplying. We will need to insert the “*” to get the program to work correctly.

from math import *
def main():
    a,b,c = eval(input("Enter the length of each side of a triangle seperated by commas "))
    s = (a+b+c)/2
    a = sqrt(s*(s-a)*(s-b)*(s-c))
    print("The area of the triangle is", a)
main()

This was actually a pretty interesting subject. I did a little bit of research to find out about it mostly because I noticed the “%” in the formula given and that means that we are talking about modulos. It’s pretty interesting. Each year is associated with an Epact. The Epact is a measure of the age of the moon (i.e. the number of days that have passed since an “official” new moon) on a particular date. The number 8 is a constant that calibrates the starting point of the Gregorian Epact so that it matches the actual age of the moon on new year’s day. Actually, this constant should have been 9, but 8 was probably chosen as a safety precaution; the calculation was known to be inaccurate, and the sentiment was that it was better to celebrate Easter too late than too early. In the Gregorian calendar, the Epact can have any value from 1 to 30

Example: What was the Epact for 1992?

GoldenNumber = 1992 mod 19 + 1 = 17
1) JulianEpact = (11 * (17-1)) mod 30 = 26
2) S = (3*20)/4 = 15
3) L = (8*20 + 5)/25 = 6
4) GregorianEpact = 26 – 15 + 6 + 8 = 25
5) No adjustment is necessary

The Epact for 1992 was 25.

8. The Gregorian epact is the number of days between January 1st and the previous new moon. This value is used to figure out the date of Easter.

There is a code given in the book, but we will have to modify it slightly to allow Python to read it correctly. The original program in the book is
c = year//100
epact = (8+(c//4) – c + (8c + 13)//25) + 11(year%19))%30
The reason we have to change this, is because python doesn’t recognize multiplication with the “*”. If you have a number next to a variable, it will come back as invalid syntax. As well, in :11(year%19)” You cannot put a number or variable before a parenthesis to multiply it because python reads that as a “function” instead of reading it as a multiplication problem. There will be more info on functions later. Be sure to keep this in mind with whatever programs you might write.

from math import *
def main():
    year = eval(input("Please enter the 4-digit year "))
    c = year//100
    epact = (8+(c//4) - c + (((8*c) + 13)//25) + 11*(year%19))%30
    print("The Gregorian epact is", epact)
main()

7. Write a program that accepts two points and determines the distance between them.

We must import the math library to use the “sqrt” function.


from math import *
def main():
    pt1x, pt1y = eval(input("Input the first point seperated by a comma "))
    pt2x, pt2y = eval(input("Input the second point seperated by a comma "))
    dis = sqrt(((pt2x-pt1x)**2) + ((pt2y - pt1y)**2))
    print("The distance between the two points is", dis)
main()

6. Two points in a plane are specified using the coordinates (x1,y1) and (x2,y2). Write a program that calculates the slope of a line through two (non-vertical) points entered by the user.

def main():
    pt1x, pt1y = eval(input("Input the first point seperated by a comma "))
    pt2x, pt2y = eval(input("Input the second point seperated by a comma "))
    sloy = pt2y - pt1y
    slox = pt2x - pt1x
    print("The slope is",sloy,"/",slox)
main()

5. The Konditorie coffee shop sells coffee at $10.50 a pound plus the cost of shipping. Each order ships for $0.86 per pound + $1.50 fixed cost for overhead. Write a program that calculates the cost of an order.

def main():
    cof = 10.50
    ship = 0.86
    ovh = 1.50
    pds = eval(input("How many pounds of coffee? "))
    ans = ovh + (cof*pds) + (ship*pds)
    print("The cost of your order would equal $",ans)
main()

My math may be off on this subject. If you see a problem, let me know, I’ll be happy to fix it!

4. Write a program that determines the distance to a lightening strike based on the time elapsed between the flash and the sound of thunder. The speed of sound is approximately 1100 ft/sec and 1 mile is 5280 ft.

#Find how much time has elapsed
#Input formulas
#Convert to miles

def main():
    time = eval(input("What is the time (in seconds) elapsed between the flash and thunder? "))
    sound = 1100*time
    ans = sound/5280
    print("The distance to a lightening strike is ", ans , "miles")
main()