How to resolve the algorithm Proper divisors step by step in the Clojure programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Proper divisors step by step in the Clojure programming language
Table of Contents
Problem Statement
The proper divisors of a positive integer N are those numbers, other than N itself, that divide N without remainder. For N > 1 they will always include 1, but for N == 1 there are no proper divisors.
The proper divisors of 6 are 1, 2, and 3. The proper divisors of 100 are 1, 2, 4, 5, 10, 20, 25, and 50.
Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Proper divisors step by step in the Clojure programming language
Source code in the clojure programming language
(ns properdivisors
(:gen-class))
(defn proper-divisors [n]
" Proper divisors of n"
(if (= n 1)
[]
(filter #(= 0 (rem n %)) (range 1 n))))
;; Property divisors of numbers 1 to 20,000 inclusive
(def data (for [n (range 1 (inc 20000))]
[n (proper-divisors n)]))
;; Find Max
(defn maximal-key [k x & xs]
" Normal max-key only finds one key that produces maximum, while this function finds them all "
(reduce (fn [ys x]
(let [c (compare (k x) (k (peek ys)))]
(cond
(pos? c) [x]
(neg? c) ys
:else (conj ys x))))
[x]
xs))
(println "n\tcnt\tPROPER DIVISORS")
(doseq [n (range 1 11)]
(let [factors (proper-divisors n)]
(println n "\t" (count factors) "\t" factors)))
(def max-data (apply maximal-key (fn [[i pd]] (count pd)) data))
(doseq [[n factors] max-data]
(println n " has " (count factors) " divisors"))
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the Oz programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Program termination step by step in the Jsish programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the UNIX Shell programming language