## Maths

Discuss other useful utilities, general computing tips & tricks, Internet resources, etc.
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Maths

Practise Installment questions ( Simple Interest, Compound Interest, Amount Due in, Amount taken as loan )
Spoiler
Solve Installment Problems, Currently only solves for The equal repayment installment
Spoiler
Spoiler
Factors Of A Number
Spoiler
Time and Work by GeekDude
Spoiler
Massive Human Multiplier by Frozenthia
Spoiler
LCM
Spoiler
WeekDay On Any date (without using any built-in thing, i.e. it merely calculates the No. of ODD days and tells the Day).Though the final result is verified the comments needs to be more elaborative.
Spoiler
GCD or HCF
Spoiler
2D
Spoiler
3D (rotation only)
Spoiler
determinant ( order 3 )
Spoiler
combinations (edited)

Code: Select all

``````#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
InputBox, nCr, Enter R and N separated with comma
StringSplit, ncr, ncr, `,
if (ncr1 = 1 OR ncr2 - ncr1 = 1)
{
MsgBox % ncr2
exitapp
}
else
{
n := ncr2,
r := ncr2 - ncr1 > ncr1 ? ncr2 - ncr1 : ncr1
r1 := ncr2 - ncr1 < ncr1 ? ncr2 - ncr1 : ncr1
f_r := 1
n_r := 1
;;;calculating numerator;;;;;;;;;
loop % n - r
n_r := n_r * n, n := n -1
;;;;;;;;;calculating denominator;;;;;;;;;;
loop % ncr2 - r
f_r := f_r * r1, r1 := r1 - 1
;;;result;;;;;;;;;;;;;;;;;
MsgBox % floor(n_r / f_r)
exitapp
}``````
Vedic maths to get squares on tips
http://ahkscript.org/boards/viewtopic.php?f=6&t=231

Permutations ( 3rd post by nimda )
http://www.autohotkey.com/board/topic/7 ... n-o1-time/

Angle Between hour hand and minute hand of a clock

Code: Select all

``````InputBox, k, enter time separated by :
StringSplit, k, k, :
MsgBox % Abs(k1 * 30 + k2/2 - k2 * 6) >= 180 ? 360 - Abs(k1 * 30 + k2/2 - k2 * 6) : Abs(k1 * 30 + k2/2 - k2 * 6)
``````
Last two digit solver

Code: Select all

``````SetFormat, float, 02.0
gui, add, text, section center, LAST TWO DIGIT SOLVER (unit's digit also)
gui, add, text, section, please enter BASE:  ; Save this control's position and start a new section.
gui, add, text,, Enter POWER of base:

gui, add, edit, ys vA ; Start a new column within this section.

Gui, Add, Button, section xm default, SUBMIT

Gui, Show,, Simple Input Example
return

ButtonSUBMIT:
Gui, Submit

base := mod(a, 100)
ans := 1
;msgbox, base is %base%
;msgbox, loop will run for %B% times
loop, %B%
{
ans := ans * Base
ans := mod(ans, 100)
;msgbox, %A% raise to power %A_index% is %ans%
}
ans += 0.0
msgbox, last two digits are `n`n %ans%
return
``````
Remainder Theorum etc.

Code: Select all

``````gui, add, text, section center, REMAINDER TELLER eg. (56)^57 / 57 = 56
gui, add, text, section, please enter BASE:  ; Save this control's position and start a new section.
gui, add, text,, Enter POWER of base:
gui, add, text,, Enter the divisor:

gui, add, edit, ys vN ; Start a new column within this section.

Gui, Add, Button, section xm default, SUBMIT
Gui, Show,, Simple Input Example
return

ButtonSUBMIT:
Gui, Submit
a := 1
l := mod(n, d)
;msgbox, remainder is %l%
loop, %p%
{
m := l * a
a := mod(m, d)
;msgbox, remainder %A_index% is %a%
}
msgbox, REMAINDER OF `n`n(%n%)^%p% / %d% `n`nIS `n`n %a%
return
``````
To get square roots of perfect squares orally
Spoiler
Last edited by smorgasbord on 02 Dec 2013, 21:11, edited 37 times in total.
John ... you working ?
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

### Re: Maths

Don't you know about our awesome codebox!?
"Nothing is quieter than a loaded gun." - Heinrich Heine
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

[quote="Alibaba"]Don't you know about our awesome codebox!?

Code: Select all

``````[/quote]
John ... you working ?
TLM
Posts: 1589
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

### Re: Maths

Alibaba wrote:Don't you know about our awesome codebox!?
Agree!
Spoiler
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

@TLM thanks for telling how to do it.
@Alibaba : thanks anyways lol. :-p
John ... you working ?
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

2D

new coordinates, After Shifting of axes and rotation of axes,
I could not find any stuff related to the same on both forums.
See original post soon
John ... you working ?
strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

### Re: Maths

I could not find any stuff related to the same on both forums.
Search for "autohotkey dr_holle pinrotate," then, go to post 2.
Your theta is not correct in general. Overall I have some issues with the style that I hopefully can explain by facing another style (untested):

Code: Select all

``````x := 2
y := 0
a := 0 ; origin_x
b := 0
rot := -50
;-------------------------------------------------------
shift(x, y, a, b)
msgbox,% x "`n" y
rotate(x, y, rot)
msgbox,% x "`n" y
;-------------------------------------------------------
shift(ByRef x, ByRef y, a = 0, b = 0){
x -= a, y -= b
}
rotate(ByRef x, ByRef y, phi_in_deg = 0){
static k := atan(1)/45
s := sin(k*phi_in_deg), c := cos(k*phi_in_deg)
, z := x, x := c*x-s*y, y := s*z+c*y
}
``````
Thanks for bringing up this topic (wrong section?) and I'd like to see more content.
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

strobo wrote:
I could not find any stuff related to the same on both forums.
Search for "autohotkey dr_holle pinrotate," then, go to post 2.
Your theta is not correct in general. Overall I have some issues with the style that I hopefully can explain by facing another style (untested):

Code: Select all

``````x := 2
y := 0
a := 0 ; origin_x
b := 0
rot := -50
;-------------------------------------------------------
shift(x, y, a, b)
msgbox,% x "`n" y
rotate(x, y, rot)
msgbox,% x "`n" y
;-------------------------------------------------------
shift(ByRef x, ByRef y, a = 0, b = 0){
x -= a, y -= b
}
rotate(ByRef x, ByRef y, phi_in_deg = 0){
static k := atan(1)/45
s := sin(k*phi_in_deg), c := cos(k*phi_in_deg)
, z := x, x := c*x-s*y, y := s*z+c*y
}
``````
Thanks for bringing up this topic (wrong section?) and I'd like to see more content.
http://www.autohotkey.com/board/topic/9 ... ten-punkt/

this? i understand only english
John ... you working ?
strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

### Re: Maths

Yes, the function is plain ahk, but I already posted here sth similar, so, You can equally well skip that.
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

strobo wrote:Yes, the function is plain ahk, but I already posted here sth similar, so, You can equally well skip that.
Thanks for posting and indulging in

more stuff

3D rotation now!!
John ... you working ?
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

@strobo
friend if you also have maths stuff do post in here.

Also suggest which topics should/may/can ( all modal verbs ) be coded.

:-p
John ... you working ?
strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

### Re: Maths

If I find sth relevant for the ahk universe I will post it, though, atm I have no idea.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

### Re: Maths

This is a faster way to find the factors of a number:

Code: Select all

``````SetBatchLines, -1
SetFormat, Float, 0.0
InputBox, n,, Enter a number to find its factors.
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
MsgBox, % factors``````
This is a function to check if a number is prime:

Code: Select all

``````SetBatchLines, -1
Loop, 2000 {
if (IsPrime(A_Index))
Primes .= A_Index ","
}
MsgBox, % Primes
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
}
}``````
This thread probably belongs in the Scripts/Functions section.
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

kon wrote:This is a faster way to find the factors of a number:

Code: Select all

``````SetBatchLines, -1
SetFormat, Float, 0.0
InputBox, n,, Enter a number to find its factors.
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
MsgBox, % factors``````
This is a function to check if a number is prime:

Code: Select all

``````SetBatchLines, -1
Loop, 2000 {
if (IsPrime(A_Index))
Primes .= A_Index ","
}
MsgBox, % Primes
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
}
}``````
This thread probably belongs in the Scripts/Functions section.
I came to know about the "while" command after i coded that. I AGREE WITH YOU 100% and am THANKFUL to you for pointing that.

I shall rewrite the code again
John ... you working ?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

### Re: Maths

Hi smorgasbord,
I want to explain why the code I posted for finding the factors of a number is fast. For each factor it finds, it adds two factors to the list of factors: f and n/f. This means that it only needs to check Sqrt(n) numbers, instead of (n/2) numbers. This makes for a big difference in speed, especially with large numbers. For example, to find the factors of 1234567890, my function checks Sqrt(n) = 35136 numbers which takes about 350ms on my PC. But (n/2) = 617283945 requires many more calculations and takes about 7.5 minutes. And as the numbers get larger the time gap increases.

Also, I should note that the admins seem to be working on the codeboxes, so the built in variables are not displaying properly currently. ex. in the "IsPrime" function that I posted above "hotkey.com/docs/Variables.htm#A_Index">A_Index" should just be "A_Index".
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

### Re: Maths

kon wrote:Hi smorgasbord,
I want to explain why the code I posted for finding the factors of a number is fast. For each factor it finds, it adds two factors to the list of factors: f and n/f. This means that it only needs to check Sqrt(n) numbers, instead of (n/2) numbers. This makes for a big difference in speed, especially with large numbers. For example, to find the factors of 1234567890, my function checks Sqrt(n) = 35136 numbers which takes about 350ms on my PC. But (n/2) = 617283945 requires many more calculations and takes about 7.5 minutes. And as the numbers get larger the time gap increases.

Also, I should note that the admins seem to be working on the codeboxes, so the built in variables are not displaying properly currently. ex. in the "IsPrime" function that I posted above "hotkey.com/docs/Variables.htm#A_Index">A_Index" should just be "A_Index".
i think i could further optimize it.. in a couple of hours

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

### Re: Maths

IsPrime optimization, kindly tell if it can further be improved, checked for a very large prime number i.e. 6746328388801 , kindly put that in input box
@Kon ; i used exactly the same method in the code that u suggested but please tell if i made any drastic change. I am using the array method to find out
eagerly waiting

kon's method Vs. Array method
Spoiler
Array method Vs. Kon's method
Spoiler
On my laptop array method takes about 11 seconds whereas the method in which we check for the divisor when it is just less than or equal to sqrt(number_to_be_checked) takes 12 seconds,

John ... you working ?
Avi
Posts: 193
Joined: 30 Sep 2013, 09:51
GitHub: aviaryan
Location: India
Contact:

### Re: Maths

Related: Scientific Maths Library - Functions for infinite calculation and precision
Writes at Dev Letters

Clipjump Clipboard Manager : More Scripts (updated 2019)

strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

### Re: Maths

Please remove the spoilers and only use code tags.
The 3D code is MUCH better formatted. Though, the (rotation) formulas are not valid.
Last edited by strobo on 27 Oct 2013, 04:45, edited 1 time in total.
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34