aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/paulo-custodio/basic
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-003/paulo-custodio/basic')
-rw-r--r--challenge-003/paulo-custodio/basic/ch-1.bas148
-rw-r--r--challenge-003/paulo-custodio/basic/ch-2.bas26
2 files changed, 174 insertions, 0 deletions
diff --git a/challenge-003/paulo-custodio/basic/ch-1.bas b/challenge-003/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..83b3973361
--- /dev/null
+++ b/challenge-003/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,148 @@
+' Challenge 003
+'
+' Challenge #1
+' Create a script to generate 5-smooth numbers, whose prime divisors are less
+' or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more
+' information, please check this wikipedia.
+
+' deque of integers
+type ElemType
+ nValue as integer
+ pNext as ElemType ptr
+end type
+
+type QType
+ pFront as ElemType ptr
+ pBack as ElemType ptr
+end type
+
+function QEmpty(byref q as QType) as boolean
+ if q.pFront = 0 then
+ QEmpty = true
+ else
+ QEmpty = false
+ end if
+end function
+
+function QFront(byref q as QType) as integer
+ QFront = q.pFront->nValue
+end function
+
+function QBack(byref q as QType) as integer
+ QBack = q.pBack->nValue
+end function
+
+sub QPushBack(byref q as QType, nValue as integer)
+ dim pElem as ElemType ptr
+
+ pElem = callocate(1, sizeof(ElemType))
+ pElem->nValue = nValue
+
+ if q.pFront = 0 then
+ q.pFront = pElem
+ q.pBack = pElem
+ else
+ q.pBack->pNext = pElem
+ q.pBack = pElem
+ end if
+end sub
+
+sub QPopBack(byref q as QType)
+ dim pElem as ElemType ptr
+
+ if q.pFront = 0 then ' empty
+ elseif q.pFront = q.pBack then ' only one element
+ deallocate q.pFront
+ q.pFront = 0
+ q.pBack = 0
+ else ' more than one element
+ pElem = q.pFront
+ do while pElem->pNext <> q.pBack
+ pElem = pElem->pNext
+ loop
+ deallocate q.pBack
+ q.pBack = pElem
+ pElem->pNext = 0
+ end if
+end sub
+
+sub QPushFront(byref q as QType, nValue as integer)
+ dim pElem as ElemType ptr
+
+ pElem = callocate(1, sizeof(ElemType))
+ pElem->nValue = nValue
+
+ if q.pFront = 0 then
+ q.pFront = pElem
+ q.pBack = pElem
+ else
+ pElem->pNext = q.pFront
+ q.pFront = pElem
+ end if
+end sub
+
+sub QPopFront(byref q as QType)
+ dim pElem as ElemType ptr
+
+ if q.pFront = 0 then ' empty
+ elseif q.pFront = q.pBack then ' only one element
+ deallocate q.pFront
+ q.pFront = 0
+ q.pBack = 0
+ else ' more than one element
+ pElem = q.pFront
+ q.pFront = pElem->pNext
+ deallocate pElem
+ end if
+end sub
+
+
+function min(a as integer, b as integer) as integer
+ if a < b then
+ min = a
+ else
+ min = b
+ end if
+end function
+
+function min3(a as integer, b as integer, c as integer) as integer
+ min3 = min(a, min(b, c))
+end function
+
+
+' Hamming generator
+dim shared q2 as QType
+dim shared q3 as QType
+dim shared q5 as QType
+
+function next_hamming() as integer
+ dim n as integer
+
+ ' init queues at start
+ if QEmpty(q2) then
+ QPushBack q2, 1
+ QPushBack q3, 1
+ QPushBack q5, 1
+ end if
+
+ ' get the smallest of the queue heads
+ n = min3(QFront(q2), QFront(q3), QFront(q5))
+
+ ' shift used multiples
+ if n = QFront(q2) then QPopFront q2
+ if n = QFront(q3) then QPopFront q3
+ if n = QFront(q5) then QPopFront q5
+
+ ' push next multiples
+ QPushBack q2, 2*n
+ QPushBack q3, 3*n
+ QPushBack q5, 5*n
+
+ next_hamming = n
+end function
+
+' main
+dim i as integer
+for i=1 to val(command(1))
+ print trim(str(next_hamming()))
+next i
diff --git a/challenge-003/paulo-custodio/basic/ch-2.bas b/challenge-003/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..5987ed7ee5
--- /dev/null
+++ b/challenge-003/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,26 @@
+' Challenge 003
+'
+' Challenge #2
+' Create a script that generates Pascal Triangle. Accept number of rows from
+' the command line. The Pascal Triangle should have at least 3 rows. For more
+' information about Pascal Triangle, check this wikipedia page.
+
+sub draw_pascal(rows as integer)
+ dim n(1, rows) as integer
+ dim cur as integer, row as integer, col as integer
+
+ cur = 0
+ n(cur, 0) = 1
+ for row=1 to rows
+ ' print current row
+ print space(rows-row);
+ for col=0 to row-1: print trim(str(n(cur,col)));" ";: next col: print
+
+ ' compute next row
+ n(1-cur, 0) = 1: n(1-cur,row) = 1
+ for col=1 to row-1: n(1-cur,col) = n(cur,col-1)+n(cur,col): next col
+ cur = 1-cur
+ next row
+end sub
+
+draw_pascal val(command(1))