Skip to content

Instantly share code, notes, and snippets.

@alecthegeek
Last active September 17, 2017 00:27
Show Gist options
  • Save alecthegeek/55c4cc953dfe220854351a6bfa0b7cfb to your computer and use it in GitHub Desktop.
Save alecthegeek/55c4cc953dfe220854351a6bfa0b7cfb to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
=begin Display the multiplication tables
My first computer program, written in Sept 1978, did the same
thing. I did not know about iteration using for or while and
so it was implemented using if and goto :-(. This was on an
ICL mainframe using MAXIMOP BASIC.
As you can see computer science, and I, have moved on since.
For many years this was the 1st computer program I wrote when
learning a new language.
Notice no support for command line arguements. That always used to
very difficult.
=end
LOWER=2 # lowest valued mult table to print
UPPER=12 # highers valued mult table to print
TABLESIZE=12 # How big will each multiplication table be
puts("\n\t\t\tMultiplication tables from #{LOWER} to #{UPPER}")
(LOWER..UPPER).each do |i|
puts("\n#{i} Times Table")
(1..TABLESIZE).each do |j|
puts("\t#{j} X #{i} = #{j*i}")
end
end
#!/usr/bin/env python3
""" Display the multiplication tables
My first computer program, written in Sept 1978, did the same
thing. I did not know about iteration using for or while and
so it was implemented using if and goto :-(. This was on an
ICL mainframe using MAXIMOP BASIC.
As you can see computer science, and I, have moved on since.
For many years this was the 1st computer program I wrote when
learning a new language.
Notice no support for command line arguements. That always used to
very difficult.
"""
lower=2 # lowest valued mult table to print
upper=12 # highers valued mult table to print
tableSize=12 # How big will each multiplication table be
def calcAndFormat(a,b):
return "\t{} X {} = {}".format(a,b,a*b)
def formatTableHeader(a):
return "\n{} Times Table".format(a)
def formtMainHeader(m,n):
return "\n\t\t\tMultiplication tables from {} to {}".format(n,m)
# main
print(formtMainHeader(upper,lower))
for i in range(lower,upper+1): # Range is exclusive at top so add one
print(formatTableHeader(i))
for j in range (1,tableSize+1):
print(calcAndFormat(j,i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment