Sequence: smallest number with exactly n divisors: Difference between revisions
(→{{header|REXX}}: flushed out the REXX stub.) |
(→{{header|REXX}}: added optimization (but not really needed for only finding 15 numbers).) |
||
Line 95: | Line 95: | ||
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/ |
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/ |
||
say '──divisors── ──smallest number with N divisors──' /*display title for the numbers.*/ |
say '──divisors── ──smallest number with N divisors──' /*display title for the numbers.*/ |
||
@.= /*the @ array is used for memoization*/ |
|||
do i=1 for N /*step through a number of divisors. */ |
do i=1 for N /*step through a number of divisors. */ |
||
do j=1 |
do j=1+(i\==1) by 1+(i\==1) /*now, search for a number that ≡ #divs*/ |
||
if @.j==. then iterate /*has this number already been found? */ |
|||
d= #divs(j); if d\==i then iterate /*get # divisors; Is not equal? Skip.*/ |
d= #divs(j); if d\==i then iterate /*get # divisors; Is not equal? Skip.*/ |
||
say center(i, 12) right(j, 19) /*display the #divs and the smallest #.*/ |
say center(i, 12) right(j, 19) /*display the #divs and the smallest #.*/ |
||
@.j=. /*mark as having found #divs for this J*/ |
|||
leave /*found a number, so now get the next I*/ |
leave /*found a number, so now get the next I*/ |
||
end /*j*/ |
end /*j*/ |
||
end /*i*/ |
end /*i*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
Revision as of 17:18, 11 April 2019
Calculate the sequence where each term an is the smallest natural number that has exactly n divisors.
- Task
Show here, on this page, at least the first 15 terms of the sequence.
- See also
- Related tasks
Go
<lang go>package main
import "fmt"
func countDivisors(n int) int {
count := 0 for i := 1; i*i <= n; i++ { if n%i == 0 { if i == n/i { count++ } else { count += 2 } } } return count
}
func main() {
const max = 15 seq := make([]int, max) fmt.Println("The first", max, "terms of the sequence are:") for i, n := 1, 0; n < max; i++ { if k := countDivisors(i); k <= max && seq[k-1] == 0 { seq[k-1] = i n++ } } fmt.Println(seq)
}</lang>
- Output:
The first 15 terms of the sequence are: [1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144]
Perl
<lang perl>use strict; use warnings; use ntheory 'divisors';
print "First 15 terms of OEIS: A005179\n"; for my $n (1..15) {
my $l = 0; while (++$l) { print "$l " and last if $n == divisors($l); }
}</lang>
- Output:
First 15 terms of OEIS: A005179 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
Perl 6
<lang perl6>sub div-count (\x) {
return 2 if x.is-prime; +flat (1 .. x.sqrt.floor).map: -> \d { unless x % d { my \y = x div d; y == d ?? y !! (y, d) } }
}
my $limit = 15;
put "First $limit terms of OEIS:A005179"; put (1..$limit).map: -> $n { first { $n == .&div-count }, 1..Inf };
</lang>
- Output:
First 15 terms of OEIS:A005179 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
REXX
<lang rexx>/*REXX program finds and displays the smallest number with exactly N divisors.*/ parse arg N . /*obtain optional argument from the CL.*/ if N== | N=="," then N= 15 /*Not specified? Then use the default.*/ say '──divisors── ──smallest number with N divisors──' /*display title for the numbers.*/ @.= /*the @ array is used for memoization*/
do i=1 for N /*step through a number of divisors. */ do j=1+(i\==1) by 1+(i\==1) /*now, search for a number that ≡ #divs*/ if @.j==. then iterate /*has this number already been found? */ d= #divs(j); if d\==i then iterate /*get # divisors; Is not equal? Skip.*/ say center(i, 12) right(j, 19) /*display the #divs and the smallest #.*/ @.j=. /*mark as having found #divs for this J*/ leave /*found a number, so now get the next I*/ end /*j*/ end /*i*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/
- divs: procedure; parse arg x 1 y /*X and Y: both set from 1st argument.*/
if x<7 then do /*handle special cases for numbers < 7.*/ if x<3 then return x /* " " " " one and two.*/ if x<5 then return x - 1 /* " " " " three & four*/ if x==5 then return 2 /* " " " " five. */ if x==6 then return 4 /* " " " " six. */ end odd= x // 2 /*check if X is odd or not. */ if odd then do; #= 1; end /*Odd? Assume Pdivisors count of 1.*/ else do; #= 3; y= x%2; end /*Even? " " " " 3.*/ /* [↑] start with known num of Pdivs.*/ do k=3 for x%2-3 by 1+odd while k<y /*for odd numbers, skip evens.*/ if x//k==0 then do /*if no remainder, then found a divisor*/ #=#+2; y=x%k /*bump # Pdivs, calculate limit Y. */ if k>=y then do; #= #-1; leave; end /*limit?*/ end /* ___ */ else if k*k>x then leave /*only divide up to √ x */ end /*k*/ /* [↑] this form of DO loop is faster.*/ return #+1 /*bump "proper divisors" to "divisors".*/</lang>
- output when using the default input:
──divisors── ──smallest number with N divisors── 1 1 2 2 3 4 4 6 5 16 6 12 7 64 8 24 9 36 10 48 11 1024 12 60 13 4096 14 192 15 144