Largest proper divisor of n: Difference between revisions

Add Factor
(Add Factor)
Line 192:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|Factor}}==
Observations:
* If <code>n</code> is even, the largest proper divisor of <code>n</code> is <code>n/2</code>.
* If <code>n</code> is odd, the largest possible proper divisor of <code>n</code> is <code>n/3</code>.
* Odd numbers have only odd divisors. Hence, we only need to search for odd divisors.
<br>
Therefore...
 
* If <code>n</code> is even, the largest proper divisor of <code>n</code> is <code>n>>1</code>.
* If <code>n</code> is odd, start searching downward toward <code>1</code> in increments of <code>2</code> starting at <code>NO(floor(n/3) - 1)</code> where <code>NO</code> means the next odd number.
<br>
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: grouping kernel math math.bitwise math.functions
math.ranges prettyprint sequences ;
 
: odd ( m -- n )
dup 3 /i 1 - next-odd 1 -2 <range>
[ divisor? ] with find nip ;
 
: largest ( m -- n ) dup odd? [ odd ] [ 2/ ] if ;
 
100 [1,b] [ largest ] map 10 group simple-table.</lang>
{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|FOCAL}}==
1,827

edits