How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the BASIC256 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the BASIC256 programming language

Table of Contents

Problem Statement

Note:   Niven   numbers are also called   Harshad   numbers.

Niven numbers are positive integers which are evenly divisible by the sum of its digits   (expressed in base ten). Evenly divisible   means   divisible with no remainder.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the BASIC256 programming language

Source code in the basic256 programming language

function digit_sum(n, sum)
	# devuelve la suma de los dígitos de n dada la suma de los dígitos de n - 1
	sum ++
	while (n > 0 and n mod 10 = 0)
		sum -= 9
		n = int(n / 10)
	end while
	return sum
end function

function divisible(n, d)
	if ((d mod 1) = 0) and ((n mod 1) = 1) then
		return 0
	end if
	return (n mod d = 0)
end function

global sum
sum = 0
gap = 0 : niven = 0 : niven_index = 0
gap_index = 0
previous  = 1
print "Gap index    Gap      Niven index     Niven number"
print "---------    ---      -----------     ------------"

for niven = 1 to 1000000
	sum = digit_sum(niven, sum)
	if divisible(niven, sum) then
		if (niven > previous + gap) then
			gap_index += 1
			gap = niven - previous
			print gap_index, gap, niven_index, previous
		end if
		previous = niven
		niven_index ++
	end if
next niven
end

  

You may also check:How to resolve the algorithm Here document step by step in the jq programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the mIRC Scripting Language programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the Tcl programming language
You may also check:How to resolve the algorithm Substring step by step in the Elixir programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Common Lisp programming language