Skip to main content

Sequences of length K having each term divisible by its preceding term

Here given code implementation process.

// C Program
// Sequences of length K having each term divisible by its preceding term
#include <stdio.h>

void subSequences(int result[], int start, 
  				  int count, int n, int k)
{
	if (count == k)
	{
		for (int i = 0; i < count; ++i)
		{
			printf(" %d", result[i]);
		}
		printf("\n");
	}
	else if (count >= k)
	{
		return;
	}
	else
	{
		for (int i = start; i <= n; ++i)
		{
			// Collect elements 
			result[count] = i;
			if (count == 0 || 
                (count > 0 && (result[count] % result[count - 1]) == 0))
			{
				subSequences(result, i, count + 1, n, k);
			}
		}
	}
}
// Handles the request to find resultant subsequences
void findSequence(int n, int k)
{
	if (n <= 0)
	{
		return;
	}
	// Auxiliary array which is collect result
	int result[k];
	subSequences(result, 1, 0, n, k);
}
int main()
{
	// 1..5
	int n = 5;
	// Length of sequences
	int k = 4;
	// Test
	/*
	    1 1 1 1     1 1 1 1
	                │⤴ ⤴⤴   

	              divisible from 
	              preceding sequences

	    1 1 1 2      
	    1 1 1 3
	    1 1 1 4
	    1 1 1 5
	    1 1 2 2
	    1 1 2 4
	    1 1 3 3     1 1 3 3  
	                │⤴ ⤴⤴   

	              divisible from 
	              preceding sequences


	    1 1 4 4
	    1 1 5 5
	    1 2 2 2
	    1 2 2 4
	    1 2 4 4
	    1 3 3 3
	    1 4 4 4
	    1 5 5 5
	    2 2 2 2
	    2 2 2 4
	    2 2 4 4
	    2 4 4 4
	    3 3 3 3
	    4 4 4 4
	    5 5 5 5
	 -------------
	*/
	findSequence(n, k);
	return 0;
}

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
// Java program for
// Sequences of length K having each term 
// divisible by its preceding term
public class Combination
{
	public void subSequences(int[] result, 
  			int start, 
  			int count, 
            int n, 
             int k)
	{
		if (count == k)
		{
			for (int i = 0; i < count; ++i)
			{
				System.out.print(" " + result[i]);
			}
			System.out.print("\n");
		}
		else if (count >= k)
		{
			return;
		}
		else
		{
			for (int i = start; i <= n; ++i)
			{
				// Collect elements 
				result[count] = i;
				if (count == 0 || 
                    (count > 0 && (result[count] % result[count - 1]) == 0))
				{
					subSequences(result, i, count + 1, n, k);
				}
			}
		}
	}
	// Handles the request to find resultant subsequences
	public void findSequence(int n, int k)
	{
		if (n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int[] result = new int[k];
		subSequences(result, 1, 0, n, k);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		// 1..5
		int n = 5;
		// Length of sequences
		int k = 4;
		// Test
		/*
		    1 1 1 1     1 1 1 1
		                │⤴ ⤴⤴   

		              divisible from 
		              preceding sequences

		    1 1 1 2      
		    1 1 1 3
		    1 1 1 4
		    1 1 1 5
		    1 1 2 2
		    1 1 2 4
		    1 1 3 3     1 1 3 3  
		                │⤴ ⤴⤴   

		              divisible from 
		              preceding sequences


		    1 1 4 4
		    1 1 5 5
		    1 2 2 2
		    1 2 2 4
		    1 2 4 4
		    1 3 3 3
		    1 4 4 4
		    1 5 5 5
		    2 2 2 2
		    2 2 2 4
		    2 2 4 4
		    2 4 4 4
		    3 3 3 3
		    4 4 4 4
		    5 5 5 5
		 -------------
		*/
		task.findSequence(n, k);
	}
}

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Sequences of length K having each term
// divisible by its preceding term
class Combination
{
	public: void subSequences(int result[], 
      int start, int count, int n, int k)
	{
		if (count == k)
		{
			for (int i = 0; i < count; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
		else if (count >= k)
		{
			return;
		}
		else
		{
			for (int i = start; i <= n; ++i)
			{
				// Collect elements
				result[count] = i;
				if (count == 0 || 
                    (count > 0 && (result[count] % result[count - 1]) == 0))
				{
					this->subSequences(result, i, count + 1, n, k);
				}
			}
		}
	}
	// Handles the request to find resultant subsequences
	void findSequence(int n, int k)
	{
		if (n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int result[k];
		this->subSequences(result, 1, 0, n, k);
	}
};
int main()
{
	Combination *task = new Combination();
	// 1..5
	int n = 5;
	// Length of sequences
	int k = 4;
	// Test
	/*
	    1 1 1 1     1 1 1 1
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 1 2      
	    1 1 1 3
	    1 1 1 4
	    1 1 1 5
	    1 1 2 2
	    1 1 2 4
	    1 1 3 3     1 1 3 3  
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 4 4
	    1 1 5 5
	    1 2 2 2
	    1 2 2 4
	    1 2 4 4
	    1 3 3 3
	    1 4 4 4
	    1 5 5 5
	    2 2 2 2
	    2 2 2 4
	    2 2 4 4
	    2 4 4 4
	    3 3 3 3
	    4 4 4 4
	    5 5 5 5
	 -------------
	*/
	task->findSequence(n, k);
	return 0;
}

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
// Include namespace system
using System;
// Csharp program for
// Sequences of length K having each term
// divisible by its preceding term
public class Combination
{
	public void subSequences(int[] result, int start, 
  							 int count, int n, int k)
	{
		if (count == k)
		{
			for (int i = 0; i < count; ++i)
			{
				Console.Write(" " + result[i]);
			}
			Console.Write("\n");
		}
		else if (count >= k)
		{
			return;
		}
		else
		{
			for (int i = start; i <= n; ++i)
			{
				// Collect elements
				result[count] = i;
				if (count == 0 || (count > 0 && 
                                   (result[count] % result[count - 1]) == 0))
				{
					this.subSequences(result, i, count + 1, n, k);
				}
			}
		}
	}
	// Handles the request to find resultant subsequences
	public void findSequence(int n, int k)
	{
		if (n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		int[] result = new int[k];
		this.subSequences(result, 1, 0, n, k);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		// 1..5
		int n = 5;
		// Length of sequences
		int k = 4;
		// Test
		/*
		    1 1 1 1     1 1 1 1
		                │⤴ ⤴⤴   
		              divisible from 
		              preceding sequences
		    1 1 1 2      
		    1 1 1 3
		    1 1 1 4
		    1 1 1 5
		    1 1 2 2
		    1 1 2 4
		    1 1 3 3     1 1 3 3  
		                │⤴ ⤴⤴   
		              divisible from 
		              preceding sequences
		    1 1 4 4
		    1 1 5 5
		    1 2 2 2
		    1 2 2 4
		    1 2 4 4
		    1 3 3 3
		    1 4 4 4
		    1 5 5 5
		    2 2 2 2
		    2 2 2 4
		    2 2 4 4
		    2 4 4 4
		    3 3 3 3
		    4 4 4 4
		    5 5 5 5
		 -------------
		*/
		task.findSequence(n, k);
	}
}

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
package main
import "fmt"
// Go program for
// Sequences of length K having each term
// divisible by its preceding term
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
func(this Combination) subSequences(result[] int, 
	start int, 
	count int, 
	n int, 
	k int) {
	if count == k {
		for i := 0 ; i < count ; i++ {
			fmt.Print(" ", result[i])
		}
		fmt.Print("\n")
	} else if count >= k {
		return
	} else {
		for i := start ; i <= n ; i++ {
			// Collect elements
			result[count] = i
			if count == 0 || 
			(count > 0 && (result[count] % result[count - 1]) == 0) {
				this.subSequences(result, i, count + 1, n, k)
			}
		}
	}
}
// Handles the request to find resultant subsequences
func(this Combination) findSequence(n, k int) {
	if n <= 0 {
		return
	}
	// Auxiliary array which is collect result
	var result = make([] int, k)
	this.subSequences(result, 1, 0, n, k)
}
func main() {
	var task * Combination = getCombination()
	// 1..5
	var n int = 5
	// Length of sequences
	var k int = 4
	// Test
	/*
	    1 1 1 1     1 1 1 1
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 1 2      
	    1 1 1 3
	    1 1 1 4
	    1 1 1 5
	    1 1 2 2
	    1 1 2 4
	    1 1 3 3     1 1 3 3  
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 4 4
	    1 1 5 5
	    1 2 2 2
	    1 2 2 4
	    1 2 4 4
	    1 3 3 3
	    1 4 4 4
	    1 5 5 5
	    2 2 2 2
	    2 2 2 4
	    2 2 4 4
	    2 4 4 4
	    3 3 3 3
	    4 4 4 4
	    5 5 5 5
	 -------------
	*/
	task.findSequence(n, k)
}

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
<?php
// Php program for
// Sequences of length K having each term
// divisible by its preceding term
class Combination
{
	public	function subSequences($result, 
                                   $start, 
                                   $count, 
                                   $n, 
                                   $k)
	{
		if ($count == $k)
		{
			for ($i = 0; $i < $count; ++$i)
			{
				echo(" ".$result[$i]);
			}
			echo("\n");
		}
		else if ($count >= $k)
		{
			return;
		}
		else
		{
			for ($i = $start; $i <= $n; ++$i)
			{
				// Collect elements
				$result[$count] = $i;
				if ($count == 0 || 
                    ($count > 0 && ($result[$count] % $result[$count - 1]) == 0))
				{
					$this->subSequences($result, $i, $count + 1, $n, $k);
				}
			}
		}
	}
	// Handles the request to find resultant subsequences
	public	function findSequence($n, $k)
	{
		if ($n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		$result = array_fill(0, $k, 0);
		$this->subSequences($result, 1, 0, $n, $k);
	}
}

function main()
{
	$task = new Combination();
	// 1..5
	$n = 5;
	// Length of sequences
	$k = 4;
	// Test
	/*
	    1 1 1 1     1 1 1 1
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 1 2      
	    1 1 1 3
	    1 1 1 4
	    1 1 1 5
	    1 1 2 2
	    1 1 2 4
	    1 1 3 3     1 1 3 3  
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 4 4
	    1 1 5 5
	    1 2 2 2
	    1 2 2 4
	    1 2 4 4
	    1 3 3 3
	    1 4 4 4
	    1 5 5 5
	    2 2 2 2
	    2 2 2 4
	    2 2 4 4
	    2 4 4 4
	    3 3 3 3
	    4 4 4 4
	    5 5 5 5
	 -------------
	*/
	$task->findSequence($n, $k);
}
main();

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
// Node JS program for
// Sequences of length K having each term
// divisible by its preceding term
class Combination
{
	subSequences(result, start, count, n, k)
	{
		if (count == k)
		{
			for (var i = 0; i < count; ++i)
			{
				process.stdout.write(" " + result[i]);
			}
			process.stdout.write("\n");
		}
		else if (count >= k)
		{
			return;
		}
		else
		{
			for (var i = start; i <= n; ++i)
			{
				// Collect elements
				result[count] = i;
				if (count == 0 || (count > 0 && 
                                   (result[count] % result[count - 1]) == 0))
				{
					this.subSequences(result, i, count + 1, n, k);
				}
			}
		}
	}
	// Handles the request to find resultant subsequences
	findSequence(n, k)
	{
		if (n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result = Array(k).fill(0);
		this.subSequences(result, 1, 0, n, k);
	}
}

function main()
{
	var task = new Combination();
	// 1..5
	var n = 5;
	// Length of sequences
	var k = 4;
	// Test
	/*
	    1 1 1 1     1 1 1 1
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 1 2      
	    1 1 1 3
	    1 1 1 4
	    1 1 1 5
	    1 1 2 2
	    1 1 2 4
	    1 1 3 3     1 1 3 3  
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 4 4
	    1 1 5 5
	    1 2 2 2
	    1 2 2 4
	    1 2 4 4
	    1 3 3 3
	    1 4 4 4
	    1 5 5 5
	    2 2 2 2
	    2 2 2 4
	    2 2 4 4
	    2 4 4 4
	    3 3 3 3
	    4 4 4 4
	    5 5 5 5
	 -------------
	*/
	task.findSequence(n, k);
}
main();

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
#  Python 3 program for
#  Sequences of length K having each term
#  divisible by its preceding term
class Combination :
	def subSequences(self, result, start, count, n, k) :
		if (count == k) :
			i = 0
			while (i < count) :
				print(" ", result[i], end = "")
				i += 1
			
			print(end = "\n")
		elif (count >= k) :
			return
		else :
			i = start
			while (i <= n) :
				#  Collect elements
				result[count] = i
				if (count == 0 or(count > 0 and 
                    (result[count] % result[count - 1]) == 0)) :
					self.subSequences(result, i, count + 1, n, k)
				
				i += 1
			
		
	
	#  Handles the request to find resultant subsequences
	def findSequence(self, n, k) :
		if (n <= 0) :
			return
		
		#  Auxiliary list which is collect result
		result = [0] * (k)
		self.subSequences(result, 1, 0, n, k)
	

def main() :
	task = Combination()
	#  1..5
	n = 5
	#  Length of sequences
	k = 4
	#  Test
	#    1 1 1 1     1 1 1 1
	#                │⤴ ⤴⤴   
	#              divisible from 
	#              preceding sequences
	#    1 1 1 2      
	#    1 1 1 3
	#    1 1 1 4
	#    1 1 1 5
	#    1 1 2 2
	#    1 1 2 4
	#    1 1 3 3     1 1 3 3  
	#                │⤴ ⤴⤴   
	#              divisible from 
	#              preceding sequences
	#    1 1 4 4
	#    1 1 5 5
	#    1 2 2 2
	#    1 2 2 4
	#    1 2 4 4
	#    1 3 3 3
	#    1 4 4 4
	#    1 5 5 5
	#    2 2 2 2
	#    2 2 2 4
	#    2 2 4 4
	#    2 4 4 4
	#    3 3 3 3
	#    4 4 4 4
	#    5 5 5 5
	# -------------
	task.findSequence(n, k)

if __name__ == "__main__": main()

Output

  1  1  1  1
  1  1  1  2
  1  1  1  3
  1  1  1  4
  1  1  1  5
  1  1  2  2
  1  1  2  4
  1  1  3  3
  1  1  4  4
  1  1  5  5
  1  2  2  2
  1  2  2  4
  1  2  4  4
  1  3  3  3
  1  4  4  4
  1  5  5  5
  2  2  2  2
  2  2  2  4
  2  2  4  4
  2  4  4  4
  3  3  3  3
  4  4  4  4
  5  5  5  5
#  Ruby program for
#  Sequences of length K having each term
#  divisible by its preceding term
class Combination 
	def subSequences(result, start, count, n, k) 
		if (count == k) 
			i = 0
			while (i < count) 
				print(" ", result[i])
				i += 1
			end

			print("\n")
		elsif (count >= k) 
			return
		else
 
			i = start
			while (i <= n) 
				#  Collect elements
				result[count] = i
				if (count == 0 || (count > 0 && 
                                   (result[count] % result[count - 1]) == 0)) 
					self.subSequences(result, i, count + 1, n, k)
				end

				i += 1
			end

		end

	end

	#  Handles the request to find resultant subsequences
	def findSequence(n, k) 
		if (n <= 0) 
			return
		end

		#  Auxiliary array which is collect result
		result = Array.new(k) {0}
		self.subSequences(result, 1, 0, n, k)
	end

end

def main() 
	task = Combination.new()
	#  1..5
	n = 5
	#  Length of sequences
	k = 4
	#  Test
	#    1 1 1 1     1 1 1 1
	#                │⤴ ⤴⤴   
	#              divisible from 
	#              preceding sequences
	#    1 1 1 2      
	#    1 1 1 3
	#    1 1 1 4
	#    1 1 1 5
	#    1 1 2 2
	#    1 1 2 4
	#    1 1 3 3     1 1 3 3  
	#                │⤴ ⤴⤴   
	#              divisible from 
	#              preceding sequences
	#    1 1 4 4
	#    1 1 5 5
	#    1 2 2 2
	#    1 2 2 4
	#    1 2 4 4
	#    1 3 3 3
	#    1 4 4 4
	#    1 5 5 5
	#    2 2 2 2
	#    2 2 2 4
	#    2 2 4 4
	#    2 4 4 4
	#    3 3 3 3
	#    4 4 4 4
	#    5 5 5 5
	# -------------
	task.findSequence(n, k)
end

main()

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
// Scala program for
// Sequences of length K having each term
// divisible by its preceding term
class Combination()
{
	def subSequences(result: Array[Int], 
      start: Int, count: Int, 
        n: Int, k: Int): Unit = {
		if (count == k)
		{
			var i: Int = 0;
			while (i < count)
			{
				print(" " + result(i));
				i += 1;
			}
			print("\n");
		}
		else if (count >= k)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= n)
			{
				// Collect elements
				result(count) = i;
				if (count == 0 || 
                    (count > 0 && (result(count) % result(count - 1)) == 0))
				{
					subSequences(result, i, count + 1, n, k);
				}
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	def findSequence(n: Int, k: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: Array[Int] = Array.fill[Int](k)(0);
		subSequences(result, 1, 0, n, k);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		// 1..5
		var n: Int = 5;
		// Length of sequences
		var k: Int = 4;
		// Test
		/*
		    1 1 1 1     1 1 1 1
		                │⤴ ⤴⤴   
		              divisible from 
		              preceding sequences
		    1 1 1 2      
		    1 1 1 3
		    1 1 1 4
		    1 1 1 5
		    1 1 2 2
		    1 1 2 4
		    1 1 3 3     1 1 3 3  
		                │⤴ ⤴⤴   
		              divisible from 
		              preceding sequences
		    1 1 4 4
		    1 1 5 5
		    1 2 2 2
		    1 2 2 4
		    1 2 4 4
		    1 3 3 3
		    1 4 4 4
		    1 5 5 5
		    2 2 2 2
		    2 2 2 4
		    2 2 4 4
		    2 4 4 4
		    3 3 3 3
		    4 4 4 4
		    5 5 5 5
		 -------------
		*/
		task.findSequence(n, k);
	}
}

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5
// Swift 4 program for
// Sequences of length K having each term
// divisible by its preceding term
class Combination
{
	func subSequences(_ result: inout[Int], 
  		_ start: Int, 
    	_ count: Int, 
      	_ n: Int, 
        _ k: Int)
	{
		if (count == k)
		{
			var i: Int = 0;
			while (i < count)
			{
				print(" ", result[i], terminator: "");
				i += 1;
			}
			print(terminator: "\n");
		}
		else if (count >= k)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= n)
			{
				// Collect elements
				result[count] = i;
				if (count == 0 || 
                    (count > 0 && (result[count] % result[count - 1]) == 0))
				{
					self.subSequences(&result, i, count + 1, n, k);
				}
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	func findSequence(_ n: Int, _ k: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: [Int] = Array(repeating: 0, count: k);
		self.subSequences(&result, 1, 0, n, k);
	}
}
func main()
{
	let task: Combination = Combination();
	// 1..5
	let n: Int = 5;
	// Length of sequences
	let k: Int = 4;
	// Test
	/*
	    1 1 1 1     1 1 1 1
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 1 2      
	    1 1 1 3
	    1 1 1 4
	    1 1 1 5
	    1 1 2 2
	    1 1 2 4
	    1 1 3 3     1 1 3 3  
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 4 4
	    1 1 5 5
	    1 2 2 2
	    1 2 2 4
	    1 2 4 4
	    1 3 3 3
	    1 4 4 4
	    1 5 5 5
	    2 2 2 2
	    2 2 2 4
	    2 2 4 4
	    2 4 4 4
	    3 3 3 3
	    4 4 4 4
	    5 5 5 5
	 -------------
	*/
	task.findSequence(n, k);
}
main();

Output

  1  1  1  1
  1  1  1  2
  1  1  1  3
  1  1  1  4
  1  1  1  5
  1  1  2  2
  1  1  2  4
  1  1  3  3
  1  1  4  4
  1  1  5  5
  1  2  2  2
  1  2  2  4
  1  2  4  4
  1  3  3  3
  1  4  4  4
  1  5  5  5
  2  2  2  2
  2  2  2  4
  2  2  4  4
  2  4  4  4
  3  3  3  3
  4  4  4  4
  5  5  5  5
// Kotlin program for
// Sequences of length K having each term
// divisible by its preceding term
class Combination
{
	fun subSequences(result: Array < Int > , 
                      start: Int, 
                      count: Int, 
                      n: Int, 
                      k: Int): Unit
	{
		if (count == k)
		{
			var i: Int = 0;
			while (i < count)
			{
				print(" " + result[i]);
				i += 1;
			}
			print("\n");
		}
		else if (count >= k)
		{
			return;
		}
		else
		{
			var i: Int = start;
			while (i <= n)
			{
				// Collect elements
				result[count] = i;
				if (count == 0 || 
                    (count > 0 && (result[count] % result[count - 1]) == 0))
				{
					this.subSequences(result, i, count + 1, n, k);
				}
				i += 1;
			}
		}
	}
	// Handles the request to find resultant subsequences
	fun findSequence(n: Int, k: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// Auxiliary array which is collect result
		var result: Array < Int > = Array(k)
		{
			0
		};
		this.subSequences(result, 1, 0, n, k);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	// 1..5
	val n: Int = 5;
	// Length of sequences
	val k: Int = 4;
	// Test
	/*
	    1 1 1 1     1 1 1 1
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 1 2      
	    1 1 1 3
	    1 1 1 4
	    1 1 1 5
	    1 1 2 2
	    1 1 2 4
	    1 1 3 3     1 1 3 3  
	                │⤴ ⤴⤴   
	              divisible from 
	              preceding sequences
	    1 1 4 4
	    1 1 5 5
	    1 2 2 2
	    1 2 2 4
	    1 2 4 4
	    1 3 3 3
	    1 4 4 4
	    1 5 5 5
	    2 2 2 2
	    2 2 2 4
	    2 2 4 4
	    2 4 4 4
	    3 3 3 3
	    4 4 4 4
	    5 5 5 5
	 -------------
	*/
	task.findSequence(n, k);
}

Output

 1 1 1 1
 1 1 1 2
 1 1 1 3
 1 1 1 4
 1 1 1 5
 1 1 2 2
 1 1 2 4
 1 1 3 3
 1 1 4 4
 1 1 5 5
 1 2 2 2
 1 2 2 4
 1 2 4 4
 1 3 3 3
 1 4 4 4
 1 5 5 5
 2 2 2 2
 2 2 2 4
 2 2 4 4
 2 4 4 4
 3 3 3 3
 4 4 4 4
 5 5 5 5




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment