Tuesday April 18th

4/18/17 - A Bit of Rest

Today I learned

  • In C++, you can exit a programme gracefully by typing “return(0)”. Since you have int main() and it’s looking for a return int, you can insert it anywhere to exit. Sort of like system.exit or whatever in other programmes.

C++ homework done.

A few snippets of code. I didn’t include another three linked .h files here. Don’t want people to copy, etc.

C++ code

Also, what my debugger looks like when the programme is run.

debugger

Things to get done

  • Look over some code for C++ and submit (hopefully) to my Lab —> done

  • Do some more work on my Programming for Correctness work. I’m currently on Split ranges

  • Get some rest tonight, and be fresh to work on Homework 6 (due 4/23)

A Kata Solved (note: this is in Python) :)

# In this Kata, you have to find the factors of integer 
# down to the limit including the limiting number.
# If the limit is more than the integer, return an empty list

def factors(integer, limit):
      # make an empty list
      arr = [] 
      
      # if limit exceeds integer, return an empty array 
      if limit > integer:
        return []
        
        # else, find all the factors of the integer 
        # between the limit and the integer
        # push those factors to the empty array
      else:
        for i in range(limit, integer+1):
          if integer % i == 0:
            arr.append(i)
        return arr

And now, another puzzle

How many balls would you need to assemble a regular tetrahedron, if the edge of the tetrahedron consists of x balls?

Write a function that takes the value and returns the count of balls needed.

rhinotetra2

The solution is an interesting one, based on a formula called “The Tetrahedral number”

Here is a visualization of the problem

Side top view packed balls

rhinotetra

Bottom view of tetrahedron packed The last row is 4 balls long. Total is 20 for all balls counted

rhinotetrabottom

Btw I did these models in a 3D product/ architectural modelling programme I learned years ago called Rhino. This software is also used by car designers and people in the movie industry. I learned it from two persons; one of whom was an Art Director (who worked on Avatar and Star Trek) , and the other, who was a car designer (he designed grills for cars) for Honda.

tetra formula

Tetrahedral number

also, check out another favourite site of mine, Math Stack Exchange:

Math Stack Exchange Tetrahedral number

As for the solution, this is in Python, too (sorry) :)

def count_balls(x):
    // the formula is :
    return x * ((x + 1) * (x + 2)) / 6

Side note

I figured out how to re-write fractions and limits in LaTeX. Here is my code for generating the Tetrahedral formula

tetralatex

Written on April 18, 2017