def timesTables(skip=1):
# in every line n of the times table...
for n in range(1, 13, skip):
# ... the products start from n and increase by n
for product in range(n, 12*n+1, n*skip):
print(product, end=' ')
print()
def oddTimesTables():
timesTables(skip=2)
The code is explained in the comments (as much as there is to explain). It is based on the observation that each product at line n differs by its previous one by n.
The function creates a counter variable and an array with a given length, and the uses āwhileā loop to print values in the array mutiplied by the accumulative counter.
Since the definition of āefficiencyā is rather vague and a naive implementation is basically as fast as the function possibly could be, I instead decided to interpret āefficiencyā as if I were programming a more complicated mathematical table on an old CPU where memory accesses are cheap and mathematical operations (i.e. multiplications) are expensive. Therefore, my āefficientā implementation of timesTables performs the minimum required number of multiplications (i.e. the upper-triangular portion of the table), stores the result, and prints the full table from this ācompressedā list of numbers. The total number of multiplications performed is therefore (N^2 + N)/2 instead of N^2, with in increase in āefficiencyā by a factor of nearly 2 for large N.
If we want to define efficiency by minimizing the number of multiplications performed then I offer up this for your consideration:
def printTable(last, odds=False):
if odds:
numbers = range(1,last+1,2)
else:
numbers = range(1,last+1)
for i in numbers:
p = i
for j in numbers:
print('{:>4}'.format(p),end="")
p += i
if odds:
p += i
print()
def timesTables():
printTable(12)
def oddTimesTables():
printTable(12, odds=True)
timesTables()
print()
oddTimesTables()
Elegant, minimal approach that will be better explained if I show you the uncompressed version,
ls = [i * j for i in range(1,13) for j in range(1,13)]
chunks = [print(ls[x:x+12]) for x in range(0, len(ls), 12)]
Uncompressed Version:
#create an empty list
ls = []
#create multiplication table using nested loops
for i in range(1,13):
for j in range(1,13):
ls.append(i*j)
#split the table at every 12th index and print it out
for x in range(0,len(ls),12):
print(ls[x:x+12])
It took me a couple of evenings, a lunch break and some help from a friend to get this, but it was fun and frustrating.
Iād really appreciate any advice or corrections that I could make also as Iām learning Javascript.
Iām guessing that console.log(); isnāt really an output, but thatās all I know for now!
for (let i = 0; i < 12; i++) {
let timesTables = '';
for (let j = 0; j < 12; j++) {
if (j === 11) {
timesTables = timesTables + (j + 1) * (i + 1);
} else {
timesTables = timesTables + (j + 1) * (i + 1) + ' '; //initially I set it up using commas so this is currently redundant.
}
}
console.log(timesTables);
}
This is my submission to the Basic difficulty it didnāt take me long but I made it as efficient as I could and it explains itself through comments.
Itās in the swift coding language
Hereās the link: https://repl.it/NZaI/0
And the code:
// these goes up every time times tables is called
var c1 = 1
var c2 = 1
//the function that happens to make the tables show
func timesTables() {
//prints c1 times c2 before it adds more numbers to make it show 1*1 too
print(c1*c2)
if c2 == 12 {
c1 += 1
c2 = 1
} else if c2 < 12 {
c2 += 1
}
}
//this runs it until the code has printed all the numbers (144 to be exact)
while c1 != 13 {
timesTables()
}
The program uses two for loops to loop through two variables, i and j. i starts out at 1; j loops from 1 to 12, multiplying by i each time, checking the result to see if it is odd, and adding it to an array. Then the array is logged to the console and cleared, then i increments by 1 and the process repeats. It will repeat until i = 12.
//Generates a multiplication table but leaves out all even numbers.
//Set variables.
var timesNum = 0;
var numArray = [];
function oddTimesTables() {
/*Use two for loops. Increment j from 1 to 12, multiplying by i each time,
checking to see if the result is odd, and adding it to an array, then
increment i once and repeat. Repeat until i = 12. */
for(i=1; i<=12; i++) {
for(j=1; j<=12; j++) {
timesNum = j*i;
//Check to see if timesNum is odd. If it is, it is added to numArray.
if(timesNum % 2 !== 0) {
numArray.push(timesNum);
}
}
//Log a line of numbers to the console.
console.log(numArray);
//Clear the array.
numArray = [];
}
}
oddTimesTables();