文章

讓家長抓狂的小一數學題?!

圖片
問過ChatGPT,AI 也沒轍! 怎樣survive 將來的 AI threat,請繼續看⋯⋯

淺釋"蒙地卡羅"算法:用"亂數"找π值

圖片
叫"蒙地卡羅"的原因,是因為似賭場:在一段長時間內,賭局的結果應趨向預期的機會率。 例如賭大小,玩一百萬局的話,大與小應趨向各佔50%。 * * * * * 來求Pi(π),在第一象限(右上,X, Y 均為正數)隨機選四個點,可能得到下圖的結果。 其比例:           圓內的點數  除以  整個四方形的點數       =      3/4 所以,整個圓形便是乘四:  3/4 x 4 = 3 我們知道 π 是 3.14... 結果誤差只有0.14..,看來不錯。如果我們隨機選四十點、四千點、四千萬點,將得出更準確數字。 那問題來了,怎樣判定在圓內(綠色)還是圓外(紅色)? 用亂數選出 x 與 y,算出與圓心距離,如果少過或等於半徑,就是圓內了 。                     __________ 距離 = √ (x^2 + y^2) 用 javascript 程式 展示 function inCircle(x, y) {     const  radius = 1     let distance =  Math.sqrt(x * x + y * y)     return (distance <= radius)  } var inside = 0 const total = 1e6 for (let i = 0;  i < total; i++ ) {     x = Math.random()     y = Math.random()     if (inCircle(x, y))          inside ++ } pi = (inside / total) * 4 console.log('Calculated Pi,...

一世人流流長,計一計有幾長

俗語有句話:「一世人流流長」 咁究竟有幾長? 作為一個稱職嘅編程員,當然首先要考慮用幾多個位元組的integer才不會溢位。 原來,只需用15 bits,當成32,767  日數 ,除以 365,已經足夠函括 89年零260日(扣除潤年),係大部分人的歲數(超過的話當bonus啦)。 原來,人的一生唔係想像中長,int 16 都用唔哂。真要善用時間,珍惜光陰呢~

彩色生命遊戲:戈斯珀滑翔機鎗 Gosper's Glider Gun, Game of Life

圖片
  這是一個模擬細胞活動的電腦編程遊戲。我加上不同顏色使其更易明白:     黑色、空格     紅色、新生兒出現在有3個鄰居的空格     綠色、有2或3個鄰居的細胞     藍色、細胞剛離世,因為太孤獨或太擁擠(少於2個或多於3個鄰居)。 不被計算為鄰居 。 (你可能已經留意到,選色附合 RGB 在彩虹的順序)😁 The game of go is a well-known computer science game which uses simple rules to produce complex cell-like behaviors.  What makes it easier to understand?  Coloring the cells!     BLACK - empty spots     RED - new borns were at empty spots with exactly 3 neighbours.     GREEN - existing cells with 2 or 3 neighbours live on     BLUE - cells just dead due to loneliness or starved from crowdiness, less than 2 or more than 3 neighbors.  Think of BLUEs non-exist . (you might have already noticed that the colours, RGB, are selected for the life phases to match the same sequences as on a rainbow 🌈 ) the 'gun' Gosper's Glider Gun is an interesting pattern, each cycle has 60 frames, while the glider ...