May I present and initiate a funny, weird, but also serious competition here?
By means of AHK a tool for the determination of all prime numbers < 10**6 shall be created.
Conditions:
- The tool must be compiled.
- The executable must not be larger than 400 kB when compressed.
- Only AHK features may be used in the script (AHK_L, not AHK_H).
- The tool should be able to run on any Windows computer without pre-installed AHK.
- To check if a number is prime, the function Prime() (see below) must be used, other methods must not be used for this purpose.
- The script must first perform the "simple" calculation given below. Afterwards, a "smart" solution must be used, which then participates in the competition.
- In an ini file the calculation time for both methods and (for the purpose of checks) the number of prime numbers found and a checksum must be written. The list of prime numbers has to be created, but does not have to be output.
- The smart-to-simple ratio for the calculation time is the success criterion.
Who achieves the best smart-to-simple ratio and with which smart method?
As a reward for the winner I will write out a book present worth about 30 $ (€), the winner will be given the choice of the book.
PS: The extremely restrictive conditions may sound a bit strange, but they have a serious background (at least for me).
EDIT: Please do not post your code here right away, but first only your smart-to-simple ratio!
Code: Select all
; PrimeCompetition.ahk
/*
By means of AHK a tool for the determination of all prime numbers < 10**6 shall be created.
Conditions:
- The tool must be compiled.
- The executable must not be larger than 400 kB when compressed.
- Only AHK features may be used in the script (AHK_L, not AHK_H).
- The tool should be able to run on any Windows computer without pre-installed AHK.
- To check if a number is prime, the function Prime() (see below) must be used, other methods must not be used for this purpose.
- The script must first perform the "simple" calculation given below. Afterwards, a "smart" solution must be used, which then participates in the competition.
- In an ini file the calculation time for both methods and (for the purpose of checks) the number of prime numbers found and a checksum must be written. The list of prime numbers has to be created, but does not have to be output.
- The smart-to-simple ratio for the calculation time is the success criterion.
*/
b := 1000000
; With this b the calculation time for the simple method could be about 2 to 3 min.
; A smaller b could be used for tests.
Prime(n) { ; https://autohotkey.com/board/topic/47325-isprime-check-if-number-is-prime/
Loop % Floor(Sqrt(n)) -1 {
If !Mod(n, A_Index + 1)
Return 0
}
Return 1
}
IniF := A_ScriptDir . "\Prime.ini"
IniWrite, 0, %IniF%, Tim, Tsim
IniWrite, 0, %IniF%, Chk, Csim
IniWrite, 0, %IniF%, Num, Nsim
; SIMPLE METHOD
; =============
SetBatchLines, -1
SoundBeep
T := A_TickCount
n := 1 , Psim := "" , Csim := Nsim := 0
Loop
{
If (n > b - 1)
Break
If Prime(n)
Psim := Psim . n . "," , Csim := Csim + n , Nsim := Nsim + 1
n := n + 2
}
Psim := SubStr(Psim, 1, -1)
Tsim := A_TickCount - T
IniWrite, %Tsim%, %IniF%, Tim, Tsim
IniWrite, %Csim%, %IniF%, Chk, Csim
IniWrite, %Nsim%, %IniF%, Num, Nsim
SoundBeep, , 500
; SMART METHOD
; ============================
/*
INSERT HERE YOUR SMART SOLUTION!!!
==================================
Tsma, Nsma and Csma should be written to ini file.
A check by comparing Nsma and Csma with Nsim and Csim should be made.
*/
; COMPARING SMART VS. SIMPLE
; ==========================
Sma2Sim := Tsma / Tsim
MsgBox Smart to Simple ratio: %Sma2Sim%`r`n%Tsma% ms vs. %Tsim% ms