Triangular Numbers
Have you ever arranged balls into an equilateral triangle? If you do fit them into a triangle, then one with equal sides is the easiest. This is seen particularly with snooker, pool and billiards. Clearly there is a fixed number of balls for each triangle, so a triangle with 4 balls per side has 10 balls, and a triangle with 5 balls per side has 15 balls. But what if you increased the balls per side? This program explores that.
#!/usr/bin/python3None of this is new to this blog, so I'll whiz through the main points.
count = 0
triangular = 0 # total number of balls in triangle
print ("Triangular Numbers")
size = input("Maximum side of triangle (integers only)? ")
size = int(size)
for x in range(size):
count += 1
triangular = triangular + count
print("Count is " +str(count) + ", triangular is " + str(triangular))
- Variables are initialised at the start
- input() is used to get the maximum triangle size from the user
- int() converts the user's input string to a number the program can use
- A for loop repeats according to the size of the side of the triangle.
- count is increased by 1 every time the program goes through the loop
- Mathematically the formula is if the triangle has n balls per side, the number of balls in the triangle is [1 + 2+ .... + n] so this program keeps a running total until it reaches n.
- Although I briefly considered dropping the count variable and just using x, I realised that there would be problems with x starting at 0 and ending at size -1. Using the count variable gets around this.
print("Count is " +str(count) + ", triangular is " + str(triangular))
The difference between word wrap and end of line is not always obvious to humans, though it is important to Python. I hope the column width of this blog doesn't cause too many problems.
The output is:
RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test19f_triangularnumbers.py
Triangular Numbers
Maximum side of triangle (integers only)? 12
Count is 1, triangular is 1
Count is 2, triangular is 3
Count is 3, triangular is 6
Count is 4, triangular is 10
Count is 5, triangular is 15
Count is 6, triangular is 21
Count is 7, triangular is 28
Count is 8, triangular is 36
Count is 9, triangular is 45
Count is 10, triangular is 55
Count is 11, triangular is 66
Count is 12, triangular is 78
>>>
Fibonacci Numbers
This is a sequence that is found in both nature and statistics and as it is a sequence of addition it has similarities to the triangular number sequence.The sequence starts off with two numbers, 0 and 1. Add those together and put the result on the end. You now have 0, 1, 1. Repeat the process with the next two numbers at the end of the sequence, now 1 & 1, and append the result to the end of the sequence, which is now 0, 1, 1, 2. So now we take the last two numbers from the sequence....
You get the idea.
Here's the code:
count = 0
a = 1
b = 0
maxcount = input("Maximum count? ")
maxcount = int(maxcount)
print("Count is " +str(count) + " Fibonacci is " + str(a))
for count in range(maxcount):
c = a + b
b = a
a = c
print("Count is " +str(count +1) + " Fibonacci is " + str(c))
Nothing here is really new, though maybe the way I've juggled the variables, reassigning values between them is new. And the output is:
RESTART: C:\Users\John\Dropbox\Misc Programming\Python\python3\test19a_fibonacci.py
Maximum count? 7
Count is 0 Fibonacci is 1
Count is 1 Fibonacci is 1
Count is 2 Fibonacci is 2
Count is 3 Fibonacci is 3
Count is 4 Fibonacci is 5
Count is 5 Fibonacci is 8
Count is 6 Fibonacci is 13
Count is 7 Fibonacci is 21
>>>
No comments:
Post a Comment