## piramid - piramid = 1 kokonut

General FreeBASIC programming questions.
bluatigro
Posts: 652
Joined: Apr 25, 2012 10:35
Location: netherlands

### piramid - piramid = 1 kokonut

a old puzle :
you have a 4 sided piramid and a 3 sided one
they differ in size 1 kokonut
how big are the piramid's ?

error :
function sum() isn't right

? :
are there solutions above 400
how do i speed this up

Code: Select all

`'' bluatigro 8 feb 2018'' piramid'sfunction sum( a() as integer , l as integer )  dim as integer i , uit   for i = 0 to l    uit +=  a( i )  next i  return uitend functionconst as integer q = 400dim as integer qa( q ) , qb( q ) , a( q ) , b( q ) , i , n , tempfor i = 0 to q  qa( i ) = 0  qb( i ) = 0next ifor n = 1 to q  qa( n ) = n * n  a( n ) = sum( qa() , n )  temp = 0  for i = 1 to n    qb( i ) = i    temp = temp + sum( qb() , i )  next i  b( n ) = tempnext nfor i = 1 to q  for n = 1 to q    if abs( a( i ) - b( n ) ) <= 1 then      print i , a( i ) , n , b( n )    end if  next nnext isleepend`
bluatigro
Posts: 652
Joined: Apr 25, 2012 10:35
Location: netherlands

### Re: piramid - piramid = 1 kokonut

update :
a faster way

? :
how do i bigger that 63 and stil have speed

error :
i get other number's in libertyB . why ?

Code: Select all

`'' bluatigro 8 feb 2018'' piramid'sfunction f( x as ulong ) as ulong'' x!  dim as ulong uit = 1  if x > 1 and x < 60 then    uit = x * f( x - 1 )  end if  return uitend functionfunction over( a as ulong , b as ulong ) as ulong  return f( a ) / ( f( a - b ) )end functionfunction over2( a as ulong , b as ulong ) as ulong  return f( a ) / ( f( b ) * f( a - b ) )end functionconst as integer q = 63dim as ulong temp , a( q ) , b( q ) , i , nfor n = 1 to q  temp  += n * n  a( n ) = temp  b( n ) = over2( n , 3 )next nfor i = 2 to q  for n = 2 to q    if abs( a( i ) - b( n ) ) <= 1 then      print i , a( i ) , n , b( n )    end if  next nnext iprint "[ game over ]"sleepend`
integer
Posts: 391
Joined: Feb 01, 2007 16:54
Location: usa

### Re: piramid - piramid = 1 kokonut

Is the 4 side pyramid a pyramid with a base and four (4) faces and the 3 side pyramid a tetrahedron?
or is the 4side a quadrilateral and the 3 side a triangle?

Running your code does not clarify that (for me).
Is the "kokonut" a sphere or is it supposed to be a linear measurement?
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

### Re: piramid - piramid = 1 kokonut

@integer

This is my interpretation of the description given by bluatigro.
You have a four sided stack of coconuts (or cannon balls) of a certain height containing X amount of coconuts and you need to construct a three sided stack with X - 1 coconuts.
How many are needed and how high are the stacks.

3 sided: ground surface is a triangle, tetrahedron pyramid.
4 sided: ground surface is a square, square pyramidal pyramid.

1 layer: no solution 1 - 1 = 0

4 sided stack 2 layers: base 4 coconuts + 1 on top.
3 sided stack 2 layers: base 3 coconuts + 1 on top.

4 sided - 3 sided = (4+1) - (3+1) = 1 coconut, you need 5 coconuts, 4 sided is 2 layers and 3 sided is 2 layers.

4 sided stack 3 layers: 9 + 4 + 1 = 14
3 sided stack 3 layers: 6 + 3 + 1 = 10
3 sided stack 4 layers: 10 + 6 + 3 + 1 = 20

Link to PDF with formulas for both pyramids http://www2.mae.ufl.edu/~uhk/STACKING-CANNONBALLS.pdf
bluatigro
Posts: 652
Joined: Apr 25, 2012 10:35
Location: netherlands

### Re: piramid - piramid = 1 kokonut

update :
i got making piramid's in O( N )
i got checking in O( N ^ 2 )

strange :
i got different result's

rem :
the kokonut's can be replaced by any sphere shaped object

Code: Select all

`'' bluatigro 12 feb 2018'' piramid'sconst as integer q = 100dim as ulong temp , tripir( q ) , forpir( q ) , triangle( q ) , i , ntemp = 0for n = 1 to q  temp += n  triangle( n ) = tempnext ntemp = 0i = 0for n = 1 to q  temp  += n * n  forpir( n ) = temp  i += triangle( n )  tripir( n ) = inext nprint "piramid 3" ,, "piramid 4"for i = 2 to q  for n = 2 to q    if abs( tripir( i ) - forpir( n ) ) = 1 then      print i , tripir( i ) , n , forpir( n )    end if  next nnext iprint "[ game over ]"sleepend`