Radical of an integer: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added another "or") |
(→{{header|Ruby}}: Add Ruby) |
||
Line 1,394: | Line 1,394: | ||
2: 111111 |
2: 111111 |
||
1: { 1281 4097 3695 894 33 0 0 } |
1: { 1281 4097 3695 894 33 0 0 } |
||
</pre> |
|||
=={{header|Ruby}}== |
|||
Adding methods to Integers, but not globally: |
|||
<syntaxhighlight lang="ruby">module Radical |
|||
refine Integer do |
|||
require 'prime' |
|||
def radical = self == 1 ? 1 : prime_division.map(&:first).inject(&:*) |
|||
def num_uniq_prime_factors = prime_division.size |
|||
def prime_pow? = prime_division.size == 1 |
|||
end |
|||
end |
|||
using Radical |
|||
n = 50 # task 1 |
|||
puts "The radicals for the first #{n} positive integers are:" |
|||
(1..n).map(&:radical).each_slice(10){|s|puts "%4d"*s.size % s} |
|||
puts # task 2 |
|||
[99999, 499999 , 999999].each{|n| puts "Radical for %6d: %6d" % [n, n.radical]} |
|||
n = 1_000_000 # task 3 |
|||
puts "\nNumbers of distinct prime factors for integers from 1 to #{n}" |
|||
(1..n).map(&:num_uniq_prime_factors).tally.each{|kv| puts "%d: %8d" % kv } |
|||
# bonus |
|||
puts "\nNumber of primes and powers of primes less than or equal to #{n}: #{(1..n).count(&:prime_pow?)}"</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 99999: 33333 |
|||
Radical for 499999: 3937 |
|||
Radical for 999999: 111111 |
|||
Numbers of distinct prime factors for integers from 1 to 1000000 |
|||
0: 1 |
|||
1: 78734 |
|||
2: 288726 |
|||
3: 379720 |
|||
4: 208034 |
|||
5: 42492 |
|||
6: 2285 |
|||
7: 8 |
|||
Number of primes and powers of primes less than or equal to 1000000: 78734 |
|||
</pre> |
</pre> |
||