Pascal's triangle

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Pascal's triangle

04 Dec 2018, 08:45

I had a go at Pascal's triangle, people are welcome to post other versions, or links to other versions.

Code: Select all

q:: ;Pascal's triangle
vOutput := ""
VarSetCapacity(vOutput, 100000*2)
vOutput := "1`r`n"
oArray2 := [1]
;note: row starting '1 67' will overflow (it goes beyond 9223372036854775807 (0x7FFFFFFFFFFFFFFF))
Loop, 66
{
	oArray := oArray2
	oArray2 := [1]
	vOutput .= "1"
	for _, vValue in oArray
	{
		if (A_Index > 1)
		{
			vNum := vLast + vValue
			oArray2.Push(vNum)
			vOutput .= " " vNum
		}
		vLast := vValue
	}
	oArray2.Push(1)
	vOutput .= " 1`r`n"
}
Clipboard := vOutput
MsgBox, % vOutput
return

;==================================================

;combinations without repetition
;n: row, k: column
;C(n, k) = n! / (k!(n-k)!)

w:: ;Pascal's triangle
;note: row starting '1 21' will overflow (because 21!=51090942171709440000 goes beyond 9223372036854775807 (0x7FFFFFFFFFFFFFFF))
vOutput := ""
VarSetCapacity(vOutput, 100000*2)
Loop, 21
{
	vN := A_Index-1 ;row
	;vOutput .= JEE_Combin(vN, 0)
	vK := 0, vOutput .= Round(JEE_Fact(vN) / (JEE_Fact(vK)*JEE_Fact(vN-vK)))
	Loop, % vN
		;vOutput .= " " JEE_Combin(vN, A_Index)
		vK := A_Index, vOutput .= " " Round(JEE_Fact(vN) / (JEE_Fact(vK)*JEE_Fact(vN-vK)))
	vOutput .= "`r`n"
}
Clipboard := vOutput
MsgBox, % "done"
return

;==================================================

;works for 0 <= vNum <= 20, and larger numbers if JEE_BigIntFact exists
JEE_Fact(vNum)
{
	local
	static oFunc := Func("JEE_BigIntFact")
	if (vNum <= 20)
	{
		vOutput := 1
		Loop, % vNum
			vOutput *= A_Index
		return vOutput
	}
	else if (IsObject(oFunc))
		return %oFunc%(vNum)
}
*/
Link:
Pascal's triangle - Rosetta Code
https://rosettacode.org/wiki/Pascal%27s ... AutoHotkey
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
DRocks
Posts: 559
Joined: 08 May 2018, 10:20

Re: Pascal's triangle

14 Dec 2018, 06:15

Ive read about it its kinda crazy. What would you use this for?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Pascal's triangle

14 Dec 2018, 16:33

- Crazy? Tell me more.
- It was mentioned in a video about coding interviews, I'd done scripts/functions for a lot of classic maths things but not this, and I wondered at what point I'd hit the Int64 barrier using the 2 different methods, factorial/addition, and I was curious as to how quickly I could program it. And the challenge of it seemed somewhat interesting, simple but not so simple.
- Potentially, calculating a row can be useful, the coefficients can be useful for maths formulas.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
DRocks
Posts: 559
Joined: 08 May 2018, 10:20

Re: Pascal's triangle

14 Dec 2018, 20:39

Haha I like your approach, it seems like you are a really good self learner. Ive read other posts where you also try to do some things just out of curiosity. Its encouraging.

Well when I looked at the Pascals triangle it was crazy in the sense that it can produce some really cool pattern when they use binaries for 1 and 0s. Looks like a QR code.
Also a pattern with multiple triangles and inveted triangle making a super beautiful design just with maths and coloring.

So I was wondering how this could be used other than pure maths
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
GitHub: oif2003

Re: Pascal's triangle

14 Dec 2018, 22:44

I remember seeing this in a Statistics class:
https://en.wikipedia.org/wiki/Binomial_distribution
You can get the binomial expansion terms from Pascal's Triangle. It's pretty cool how you can let the variables represent the outcomes then simply raise it to whatever power and count the occurrences.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Pascal's triangle

24 Dec 2018, 04:38

- The numbers can be useful for calculating combinations/permutations with/without repetitions.
- Here's the video that led me to have a go at Pascal's triangle.
How to solve coding interview problems ("Let's leetcode") - YouTube
https://www.youtube.com/watch?v=dIrS31CCITM
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: DuyMinh and 30 guests