Programming Praxis – Tetrahedral Numbers

In today’s Programming Praxis, our goal is to find the base of the three-sided pyramid that has 169179692512835000 spheres in it. Let’s get started, shall we?

A quick import:

import Data.List

The tetrahedral numbers are based on the triangular numbers, so let’s start with those.

triangular :: [Integer]
triangular = scanl1 (+) [1..]

The tetrahedral numbers are formed in much the same way as the triangular ones.

tetrahedral :: [Integer]
tetrahedral = scanl1 (+) triangular

All that’s left to do is to is to find the base of the pyramid.

main :: IO ()
main = print . maybe 0 succ $
       findIndex (== 169179692512835000) tetrahedral
About these ads

Tags: , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: