## Maths

Discuss other useful utilities, general computing tips & tricks, Internet resources, etc.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

### Re: Maths

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
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

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 John ... you working ?
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

determinant ( order 3) solver and combination main formula calculator
John ... you working ?
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

John ... you working ?
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

John ... you working ?
Alibaba
Posts: 479
Joined: 29 Sep 2013, 16:15
Location: Germany

### Re: Maths

nice 3d rotation script.
if you add an equal offsets for each dimensions to the coordinates, you could create a nice orthographic map view.
"Nothing is quieter than a loaded gun." - Heinrich Heine
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

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  , just putting all stuff at one place, i haven't tested if it works 100%,  your second line makes no sense to a noob as myself  John ... you working ?
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

To get square roots of perfect squares orally
John ... you working ?
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

Sum of first N natural numbers, sum of their squares/cubes....till power 7
John ... you working ?
Alibaba
Posts: 479
Joined: 29 Sep 2013, 16:15
Location: Germany

### Re: Maths

smorgasbord wrote:just putting all stuff at one place, i haven't tested if it works 100%,  I tested it. Sorry, it doesn't work... "Nothing is quieter than a loaded gun." - Heinrich Heine
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

@alibaba which one?
tell soon
:-p
John ... you working ?
Alibaba
Posts: 479
Joined: 29 Sep 2013, 16:15
Location: Germany

### Re: Maths

smorgasbord wrote:@alibaba which one?
tell soon
:-p
3D rotation
"Nothing is quieter than a loaded gun." - Heinrich Heine
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

@Alibaba
http://www.siggraph.org/education/mater ... 3drota.htm
i coded as per this.
btw: Where are your forty thieves?
John ... you working ?
strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

### Re: Maths

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.
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

@Strobo and alibaba
could you kindly edit them ?
and comment the edited part. John ... you working ?
Alibaba
Posts: 479
Joined: 29 Sep 2013, 16:15
Location: Germany

### Re: Maths

smorgasbord wrote:Where are your forty thieves?
I never saw them again, since there was this guy handing out free beer.
"Nothing is quieter than a loaded gun." - Heinrich Heine
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

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       These emoticons are awesome!  John ... you working ?
Avi
Posts: 193
Joined: 30 Sep 2013, 09:51
GitHub: aviaryan
Location: India
Contact:

### Re: Maths

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.
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

@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.
John ... you working ?
nnnik
Posts: 4455
Joined: 30 Sep 2013, 01:01
Location: Germany

### Re: Maths

You'll have to repost your stuff.
Recommends AHK Studio