How to resolve the algorithm Harshad or Niven series step by step in the AWK programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Harshad or Niven series step by step in the AWK programming language

Table of Contents

Problem Statement

The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example,   42   is a Harshad number as   42   is divisible by   (4 + 2)   without remainder. Assume that the series is defined as the numbers in increasing order.

The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to:

Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Harshad or Niven series step by step in the AWK programming language

Source code in the awk programming language

#!/usr/bin/awk -f
BEGIN {
	k=0; n=0;
	printf("First twenty Harshad numbers are:\n   ");
	while (k<20) {
		if (isharshad(++n)) {
			printf("%i ",n);
			++k;
		}
	}
	n = 1000; 
	while (!isharshad(++n));
	printf("\nFirst Harshad number larger than 1000 is \n   %i\n",n);
}

function isharshad(n) {
	s = 0;
	for (i=0; i<length(n); ) {
		s+=substr(n,++i,1);
	}
	return !(n%s);
}


  

You may also check:How to resolve the algorithm N-queens problem step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the Delphi programming language
You may also check:How to resolve the algorithm Ordered words step by step in the SPL programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the ooRexx programming language