Largest proper divisor of n: Difference between revisions

Content added Content deleted
(Added Go)
Line 839: Line 839:
The core logic only concerns itself with a list of prime factors; it is never even aware of the original input integer, nor the final integer result. In fact, note that the final multiplication is implicit and never spelled out by the programmer; product is the inverse of factorization, and we requested to work "under" factorization, thus J's algebra knows to apply the inverse of factorization (i.e. taking the product) as the final step.
The core logic only concerns itself with a list of prime factors; it is never even aware of the original input integer, nor the final integer result. In fact, note that the final multiplication is implicit and never spelled out by the programmer; product is the inverse of factorization, and we requested to work "under" factorization, thus J's algebra knows to apply the inverse of factorization (i.e. taking the product) as the final step.


=={{header|jq}}==
'''Works with jq'''

'''Works with gojq, the Go implementation of jq'''

<lang jq># (1|largestpd) is 1 as per the task definition
def largestpd:
if . == 1 then 1
else . as $n
| first( range( ($n - ($n % 2)) /2; 0; -1) | (select($n % . == 0) ))
end;</lang>
<lang jq># For neatness
def lpad($len):
tostring | ($len - length) as $l | (" " * $l)[:$l] + .;

def nwise($n):
def w: if length <= $n then . else .[:$n], (.[$n:]|w) end;
w;
### The task
[range(1; 101) | largestpd]
| nwise(10) | map(lpad(2)) | join(" ")</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|Julia}}==
=={{header|Julia}}==
<lang julia>largestpd(n) = (for i in n÷2:-1:1 (n % i == 0) && return i; end; 1)
<lang julia>largestpd(n) = (for i in n÷2:-1:1 (n % i == 0) && return i; end; 1)