How to resolve the algorithm Proper divisors step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Proper divisors step by step in the Nim 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 Nim programming language

Source code in the nim programming language

import strformat

proc properDivisors(n: int) =
  var count = 0
  for i in 1..<n:
    if n mod i == 0:
      inc count
      write(stdout, fmt"{i} ")
  write(stdout, "\n")

proc countProperDivisors(n: int): int =
  var nn = n
  var prod = 1
  var count = 0
  while nn mod 2 == 0:
    inc count
    nn = nn div 2
  prod *= (1 + count)
  for i in countup(3, n, 2):
    count = 0
    while nn mod i == 0:
      inc count
      nn = nn div i
    prod *= (1 + count)
  if nn > 2:
    prod *= 2
  prod - 1

for i in 1..10:
  write(stdout, fmt"{i:2}: ")
  properDivisors(i)

var max = 0
var maxI = 1

for i in 1..20000:
  var v = countProperDivisors(i)
  if v >= max:
    max = v
    maxI = i

echo fmt"{maxI} with {max} divisors"


  

You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Nim programming language
You may also check:How to resolve the algorithm Factorial step by step in the TorqueScript programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the REBOL programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Go programming language