Page 2 of 3

Re: Maths

Posted: 11 Oct 2013, 11:56
by kon
Using "SetBatchLines, -1" will speed up the run times.
If you just want to determine if a number is prime instead of finding all the factors, the second chunk of code I posted is faster than the first. Here they are with some speed tests. The first one takes about 1 second, the second one takes about 250-300ms:

Code: Select all

#NoEnv
SetBatchLines, -1
SetFormat, Float, 0.0
n := 6746328388801
Tick := A_TickCount
f := 2, x := Sqrt(n)
while, (f <= x) {
	if (!Mod(n, f))
		factors .= f "`n" (n / f) "`n"
	f++
}
Sort, factors, N U
factors := !factors ? "Prime" : factors
Tick := A_TickCount - Tick
MsgBox, % factors "`nComputed in " Tick "ms."

Code: Select all

#NoEnv
SetBatchLines, -1
Tick := A_TickCount
p := IsPrime(6746328388801)
Tick := A_TickCount - Tick
if (p)
	MsgBox, % "The number is prime.`nComputed in " Tick "ms."
else
	MsgBox, % "The number is not prime.`nComputed in " Tick "ms."
return

IsPrime(n) {
	if (n < 2)
		return, 0
	else if (n < 4)
		return, 1
	else if (!Mod(n, 2))
		return, 0
	else if (n < 9)
		return 1
	else if (!Mod(n, 3))
		return, 0
	else {
		r := Floor(Sqrt(n))
		f := 5
		while (f <= r) {
			if (!Mod(n, f))
				return, 0
			if (!Mod(n, (f + 2)))
				return, 0
			f += 6
		}
		return, 1
	}
}
Edit: The "IsPrime" function uses the rule which says every prime except 2 and 3 can be written as n = 6k ± 1 where k is an integer and n is the prime number. So the function only has to check (at most) one third of the numbers between 0 and Sqrt(n).
Reference: https://en.wikipedia.org/wiki/Primality ... ve_methods

Re: Maths

Posted: 12 Oct 2013, 10:43
by smorgasbord
kon wrote:Using "SetBatchLines, -1" will speed up the run times.
If you just want to determine if a number is prime instead of finding all the factors, the second chunk of code I posted is faster than the first. Here they are with some speed tests. The first one takes about 1 second, the second one takes about 250-300ms:

Code: Select all

#NoEnv
SetBatchLines, -1
SetFormat, Float, 0.0
n := 6746328388801
Tick := A_TickCount
f := 2, x := Sqrt(n)
while, (f <= x) {
	if (!Mod(n, f))
		factors .= f "`n" (n / f) "`n"
	f++
}
Sort, factors, N U
factors := !factors ? "Prime" : factors
Tick := A_TickCount - Tick
MsgBox, % factors "`nComputed in " Tick "ms."

Code: Select all

#NoEnv
SetBatchLines, -1
Tick := A_TickCount
p := IsPrime(6746328388801)
Tick := A_TickCount - Tick
if (p)
	MsgBox, % "The number is prime.`nComputed in " Tick "ms."
else
	MsgBox, % "The number is not prime.`nComputed in " Tick "ms."
return

IsPrime(n) {
	if (n < 2)
		return, 0
	else if (n < 4)
		return, 1
	else if (!Mod(n, 2))
		return, 0
	else if (n < 9)
		return 1
	else if (!Mod(n, 3))
		return, 0
	else {
		r := Floor(Sqrt(n))
		f := 5
		while (f <= r) {
			if (!Mod(n, f))
				return, 0
			if (!Mod(n, (f + 2)))
				return, 0
			f += 6
		}
		return, 1
	}
}
Edit: The "IsPrime" function uses the rule which says every prime except 2 and 3 can be written as n = 6k ± 1 where k is an integer and n is the prime number. So the function only has to check (at most) one third of the numbers between 0 and Sqrt(n).
Reference: https://en.wikipedia.org/wiki/Primality ... ve_methods
beautiful it is :) keep guiding :)

Re: Maths

Posted: 17 Oct 2013, 02:58
by smorgasbord
added
determinant ( order 3) solver and combination main formula calculator

Re: Maths

Posted: 17 Oct 2013, 05:08
by smorgasbord
added Permutation stuff

Re: Maths

Posted: 19 Oct 2013, 09:40
by smorgasbord
added three more scripts,

Re: Maths

Posted: 19 Oct 2013, 11:26
by Alibaba
nice 3d rotation script.
if you add an equal offsets for each dimensions to the coordinates, you could create a nice orthographic map view.

Re: Maths

Posted: 19 Oct 2013, 11:38
by smorgasbord
Alibaba wrote:nice 3d rotation script.
if you add an equal offsets for each dimensions to the coordinates, you could create a nice orthographic map view.
Thanks :o :o , just putting all stuff at one place, i haven't tested if it works 100%, :cry: :cry:

your second line makes no sense to a noob as myself :? :?

Re: Maths

Posted: 23 Oct 2013, 10:51
by smorgasbord
added :
To get square roots of perfect squares orally

Re: Maths

Posted: 25 Oct 2013, 07:40
by smorgasbord
added:

Sum of first N natural numbers, sum of their squares/cubes....till power 7

Re: Maths

Posted: 26 Oct 2013, 07:30
by Alibaba
smorgasbord wrote:just putting all stuff at one place, i haven't tested if it works 100%, :cry: :cry:
I tested it. Sorry, it doesn't work... :(

Re: Maths

Posted: 26 Oct 2013, 08:11
by smorgasbord
@alibaba which one?
tell soon
:-p

Re: Maths

Posted: 26 Oct 2013, 11:23
by Alibaba
smorgasbord wrote:@alibaba which one?
tell soon
:-p
3D rotation

Re: Maths

Posted: 26 Oct 2013, 23:20
by smorgasbord
@Alibaba
http://www.siggraph.org/education/mater ... 3drota.htm
i coded as per this.
Please elaborate a bit :-p
btw: Where are your forty thieves?

Re: Maths

Posted: 27 Oct 2013, 06:08
by strobo
Using other vars, say, x1, y1, z1 instead of xyz on the left hand side will fix it.
Btw 2d rot was and is also wrong.

Re: Maths

Posted: 27 Oct 2013, 06:47
by smorgasbord
@Strobo and alibaba
could you kindly edit them ?
and comment the edited part.
:)
thanks in advance.

Re: Maths

Posted: 27 Oct 2013, 11:12
by Alibaba
smorgasbord wrote:Where are your forty thieves?
I never saw them again, since there was this guy handing out free beer.

Re: Maths

Posted: 27 Oct 2013, 11:17
by smorgasbord
Alibaba wrote:
smorgasbord wrote:Where are your forty thieves?
I never saw them again, since there was this guy handing out free beer.
Actually i am that beer guy! lol

:x :x :x :oops: :oops: :cry: :cry:
These emoticons are awesome! :twisted: :twisted:

Re: Maths

Posted: 30 Oct 2013, 07:54
by Avi
Sum of first N natural numbers, sum of their squares/cubes....till power 7
The code what I see is the following . I dont think this is the code you have wrote --
Spoiler
The &#40 looks to be added due to some syntax highlight problems ... Please post the correct code.

Re: Maths

Posted: 30 Oct 2013, 11:51
by smorgasbord
@AVI
Thanks for pointing that out, :(
i think that is the only place where i store stuff, so i might need some time to edit it.
i reported the same(Almost) bug in here http://ahkscript.org/boards/viewtopic.php?f=5&t=314
But as i could circumvent the bug i put it as [solved]
:(:(
Sorry.

Re: Maths

Posted: 31 Oct 2013, 09:04
by nnnik
You'll have to repost your stuff.