Proper divisors: Difference between revisions
Content added Content deleted
m (+ note in D entry) |
(J) |
||
Line 29: | Line 29: | ||
Tuple!(uint, int)(79, 18480)</pre> |
Tuple!(uint, int)(79, 18480)</pre> |
||
The Run-time is about 0.71 seconds with the ldc2 compiler. |
The Run-time is about 0.71 seconds with the ldc2 compiler. |
||
=={{header|J}}== |
|||
The proper divisors of an integer are the [[Factors of an integer]] without the integer itself. |
|||
So, borrowing from [[Factors of an integer#J|the J implementation]] of that related task: |
|||
<lang J>factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__ |
|||
properDivisors=: factors -. -.&1</lang J> |
|||
Proper divisors of numbers 1 through 10: |
|||
<lang J> (,&": ' -- ' ,&": properDivisors)&>1+i.10 |
|||
1 -- 1 |
|||
2 -- 1 |
|||
3 -- 1 |
|||
4 -- 1 2 |
|||
5 -- 1 |
|||
6 -- 1 2 3 |
|||
7 -- 1 |
|||
8 -- 1 2 4 |
|||
9 -- 1 3 |
|||
10 -- 1 2 5</lang> |
|||
Number(s) not exceeding 20000 with largest number of proper divisors (and the count of those divisors): |
|||
<lang J> (, #@properDivisors)&> 1+I.(= >./) #@properDivisors@> 1+i.20000 |
|||
15120 79 |
|||
18480 79</lang> |
|||
Note that it's more efficient to simply count factors here, when selecting the candidate numbers. |
|||
<lang J> (, #@properDivisors)&> 1+I.(= >./) #@factors@> 1+i.20000 |
|||
15120 79 |
|||
18480 79</lang> |
|||
We could also arbitrarily toss either 15120 or 18480, if that were important. |
|||
=={{header|Java}}== |
=={{header|Java}}== |