M
M
Moorlystick2019-03-21 10:55:05
Algorithms
Moorlystick, 2019-03-21 10:55:05

How to generate numbers whose sum of digits is equal to a given number?

The task is: Given an input number, output a set of numbers whose sum of digits is equal to this number, and the numbers must be different.
I ask for help in building an algorithm for generating such numbers, Or, if it has already been decided somewhere, links to this resource.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2019-03-21
@iamd503

<?php

$number = 100;
$a=1;
$b = $number - $a;

for ($i = 1; $i < $number; $i++) {
  if ($a != $b) {
    echo $a . '+';
    echo $b . '|';
    $a++;
    $b--;
  }
}

The truth does not take into account the permutation of the terms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question