0

I have this math assignment that I should make into code. I've tried all I thought of but I couldn't find a solution. All this should be done without using php functions, only math operations. You can use while, for, and such...

So I have number for example 9 Now I should create number of the length 9 which would be 999999999

If I had, for example, number 3, then the result should be 333.

Any ideas?

$gen = -1;
while($highest > 0) {
    $gen = $highest + ($highest * 10);
    $highest = $highest - 1;
}

echo $gen;
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Cadilab
  • 142
  • 10
  • what have you tried so far? do some effort and show some code. this platform is not a free source code provider. your assignment is only two lines of code. do some effort otherwise you will not become a software developer – Bilal Ahmed Feb 28 '18 at 07:00
  • Yes I have! There is some code I used. Results I got were 99, 33 but thats not what it should be. – Cadilab Feb 28 '18 at 07:03
  • Is this supposed to be math generated number or just make the number? The most obvious is strpad, but I guess you don't want that? What happens if input is 0? – Andreas Feb 28 '18 at 07:15
  • If you can accept "string math", this is probably the shortest and fastest code; https://3v4l.org/m3SEQ – Andreas Feb 28 '18 at 07:23
  • @Andreas my first thought was `str_repeat()`. `echo (int)str_repeat($n,$n);` – mickmackusa Feb 28 '18 at 12:12

3 Answers3

4

Here is a method that does not build a string; it uses pure math. (There will be many, many ways to do this task)

$x=9;
$result=0;
for($i=$x; $i; --$i){         // this looping expression can be structured however you wish potato-potatoe
   $result+=$x*(10**($i-1));  // x times (10 to the power of (i-1))
}
echo $result;
// 999999999

*note: ** acts like pow() if you want to look it up.


Late edit: here is a clever, little loopless method (quietly proud). I am only calling range() and foreach() to demo; it is not an integral component of my method.

Demo: https://3v4l.org/GIjfG

foreach(range(0,9) as $n){
    // echo "$n -> ",(integer)(1/9*$n*(10**$n)-($n/10)),"\n";
    // echo "$n -> ",(1/9*$n*(10**$n)-(1/9*$n)),"\n";
    // echo "$n -> ",(int)(1/9*10**$n)*$n,"\n";
    // echo "$n -> ",(int)(10**$n/9)*$n,"\n";
    echo "$n -> ",(10**$n-1)/9*$n,"\n";
}

Output:

0 -> 0
1 -> 1
2 -> 22
3 -> 333
4 -> 4444
5 -> 55555
6 -> 666666
7 -> 7777777
8 -> 88888888
9 -> 999999999

1/9 is the hero of this method because it generates .111111111(repeating). From this float number, I am using 10**$n to "shift" just enough 1s to the left side of the decimal point, then multiplying this float number by $n, then the float must be converted to an integer to complete.

Per @axiac's comment, the new hero is 10**$n-1 which generates a series of nines to the desired length (no float numbers). Next divide the nines by nine to generate a series of ones which becomes the perfect multiplier. Finally, multiply the series of ones and the input number to arrive at the desired output.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 2
    Yes, I agree with this. This is math. not string building. – Andreas Feb 28 '18 at 07:19
  • `**` is available only on PHP 5.6 and newer versions. – axiac Feb 28 '18 at 07:41
  • 1
    @axiac you are welcome to upgrade your version if it doesn't work for your environment. Look, I quickly mocked that up from my phone. I think it provides correct results. Certainly this is not worthy of a downvote. – mickmackusa Feb 28 '18 at 07:47
  • 2
    In my opinion, if OP does not specifically write what version he/she uses then it's OPs problem to convert functions. If OP says "I use PHP 4" then it would be bad to write an answer suited for PHP 7. In short, my opinion is a downvote for PHP version is not correct. A comment, sure. But downvote is a bit too much. – Andreas Feb 28 '18 at 08:04
  • 1
    Yes this is it! Thank you @mickmackusa – Cadilab Feb 28 '18 at 08:52
  • @Cadilab I've added a loopless solution... You may or may not be the "cool kid" tomorrow at school when you reveal this newer method. – mickmackusa Feb 28 '18 at 10:50
  • I might still refine my method when I get more time. – mickmackusa Feb 28 '18 at 11:15
  • 1
    `(10**$n-1)/9*$n` and you don't need to cast to `int` any more. – axiac Feb 28 '18 at 16:03
3

There are two operations you need to accomplish:

  1. given a number $number, append the digit $n to it;
  2. repeat operation #1 some number of times ($n times).

Operation #1 is easy:

$number = $number * 10 + $n;

Operation #2 is even easier:

for ($i = 0; $i < $n; $i ++)

What else do you need?
Initialization of the variable used to store the computed number:

$number = 0;

Put them in order and you get:

// The input digit
// It gives the length of the computed number
// and also its digits
$n = 8;

// The number we compute
$number = 0;

// Put the digit $n at the end of $number, $n times
for ($i = 0; $i < $n; $i ++) {
    $number = $number * 10 + $n;
}

// That's all
axiac
  • 68,258
  • 9
  • 99
  • 134
2

If intval() is accepted:

$result = '';
$input = 9;
for($i=0; $i < $input; $i++){
    $result .= $input;
}
$result = intval($result);

else:

$result = 0;
$input = 9;
for($i=0; $i < $input; $i++){
    $factor = 1;
    for($j = 0; $j < $i; $j++){
        $factor *= 10;
    }
    $result += $input * $factor;
}

=> 9 + 90 + 900 + 9000 + 90000...

Phillip K
  • 237
  • 1
  • 8