Radical of an integer: Difference between revisions

Content added Content deleted
(Added XPL0 example.)
(Added FreeBasic)
Line 28: Line 28:
* OEIS sequence [[oeis:A007947|A007947: Largest square free number dividing n]]
* OEIS sequence [[oeis:A007947|A007947: Largest square free number dividing n]]
<br>
<br>

=={{header|FreeBASIC}}==
{{trans|XPL0}}
<syntaxhighlight lang="vb">'#include "isprime.bas"

Function Radical(n As Integer) As Integer 'Return radical of n
Dim As Integer d = 2, d0 = 0, p = 1
While n >= d*d
While n Mod d = 0
If d <> d0 Then
p *= d
d0 = d
End If
n /= d
Wend
d += 1
Wend
If d <> d0 Then p *= n
Return p
End Function

Function DistinctFactors(n As Integer) As Integer 'Return count of distinct factors of n
Dim As Integer d = 2, d0 = 0, c = 0
While n >= d*d
While n Mod d = 0
If d <> d0 Then
c += 1
d0 = d
End If
n = n/d
Wend
d += 1
Wend
If d <> d0 And n <> 1 Then c += 1
Return c
End Function

Dim As Integer n, c, count(0 To 9), pcount, ppcount, p2, p
Print "The radicals for the first 50 positive integers are:"
For n = 1 To 50
Print Using "####"; Radical(n);
If n Mod 10 = 0 Then Print
Next
Print Using !"\nRadical for ###,###: ###,###"; 99999; Radical(99999)
Print Using "Radical for ###,###: ###,###"; 499999; Radical(499999)
Print Using "Radical for ###,###: ###,###"; 999999; Radical(999999)

For n = 0 To 9
count(n) = 0
Next
For n = 1 To 1e6
c = DistinctFactors(n)
count(c) += 1
Next
Print !"\nBreakdown of numbers of distinct prime factors \nfor positive integers from 1 to 1,000,000:"
c = 0
For n = 0 To 9
If count(n) > 0 Then Print Using " #: ###,###"; n; count(n)
c += count(n)
Next
Print Using !" ---------\n #,###,###"; c

Print !"\nor graphically:\n"; String(50, "-")
For n = 0 To 9
If count(n) > 0 Then Print n; " "; String(count(n)/1e4, Chr(177)); " "; count(n)
c += count(n)
Next
Print String(50, "-")

'Bonus (algorithm from Wren):
pcount = 0
For n = 1 To 1e6
If isPrime(n) Then pcount += 1
Next
Print !"\nFor primes or powers (>1) thereof <= 1,000,000:"
Print Using " Number of primes: ##,###"; pcount
ppcount = 0
For p = 1 To Sqr(1e6)
If isPrime(p) Then
p2 = p
Do
p2 *= p
If p2 > 1e6 Then Exit Do
ppcount += 1
Loop
End If
Next
Print Using " Number of powers: ##,###"; ppcount
Print Using "Add 1 for number 1: ##,###"; 1
If isPrime(n) Then pcount += 1
Print Spc(19); "-------"
Print Spc(13); Using !"Total: ##,###"; pcount + ppcount + 1

Sleep</syntaxhighlight>
{{out}}
<pre>The radicals for the first 50 positive integers are:
1 2 3 2 5 6 7 2 3 10
11 6 13 14 15 2 17 6 19 10
21 22 23 6 5 26 3 14 29 30
31 2 33 34 35 6 37 38 39 10
41 42 43 22 15 46 47 6 7 10

Radical for 99,999: 33,333
Radical for 499,999: 3,937
Radical for 999,999: 111,111

Breakdown of numbers of distinct prime factors
for positive integers from 1 to 1,000,000:
0: 1
1: 78,734
2: 288,726
3: 379,720
4: 208,034
5: 42,492
6: 2,285
7: 8
---------
1,000,000

or graphically:
--------------------------------------------------
0 1
1 ▒▒▒▒▒▒▒▒ 78734
2 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 288726
3 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 379720
4 ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 208034
5 ▒▒▒▒ 42492
6 2285
7 8
--------------------------------------------------

For primes or powers (>1) thereof <= 1,000,000:
Number of primes: 78,498
Number of powers: 236
Add 1 for number 1: 1
-------
Total: 78,735</pre>

=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang=J> ~.&.q: 1+i.5 10 NB. radicals of first 50 positive integers
<syntaxhighlight lang=J> ~.&.q: 1+i.5 10 NB. radicals of first 50 positive integers