Totient function: Difference between revisions

Line 1,226:
Number of primes to 100000 : 9592
</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
global cptext := a_tab "Nr" a_tab "Phi" a_tab "Prime?`n---------------------------------------------`n"
 
divisores(num)
{
serie := ""
loop % num
if !mod(num,a_index)
serie .= a_index ","
return serie
}
 
gcd(serieA,serieB)
{
emComum := 0
loop,parse,serieA,csv
if A_LoopField in %serieB%
emComum += 1
return emComum
}
principal(voltas,phi:=0)
{
loop %voltas%
{
cp := A_Index
cpcount := 0
numA := divisores(cp)
loop % a_index
{
numA := divisores(cp)
numB := divisores(A_Index)
fim := gcd(numA,numB)
if (fim = 1)
cpcount += 1
}
if (cpcount = cp-1)
{
if phi
cptext .= a_tab cp a_tab cpcount a_tab "1`n"
totalPrimes += 1
}
else
cptext .= a_tab cp a_tab cpcount a_tab "0`n"
}
return totalPrimes
}
 
totalPrimes := principal(25,1)
msgbox % cptext "`n`ntotal primes = " totalPrimes ; Number 1 is a prime number ? If yes, add 1 to totalPrimes
totalPrimes := principal(100)
msgbox % "total primes in 1 .. 100 = " totalPrimes
totalPrimes := principal(1000) ; caution...
msgbox % "total primes in 1 .. 1000 = " totalPrimes ; 3 minutes or more
;totalPrimes := principal(10000)
;msgbox % "total primes in 1 .. 10000 = " totalPrimes
ExitApp
</syntaxhighlight>
{{Out}}
<pre>
---------------------------
Totient function.ahk
---------------------------
Nr Phi Prime?
---------------------------------------------
1 1 0
2 1 1
3 2 1
4 2 0
5 4 1
6 2 0
7 6 1
8 4 0
9 6 0
10 4 0
11 10 1
12 4 0
13 12 1
14 6 0
15 8 0
16 8 0
17 16 1
18 6 0
19 18 1
20 8 0
21 12 0
22 10 0
23 22 1
24 8 0
25 20 0
 
total primes = 9
---------------------------
OK
---------------------------
total primes in 1 .. 100 = 25
---------------------------
OK
---------------------------
total primes in 1 .. 1000 = 168
---------------------------
OK
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
16

edits