Write your multiplying by powers of 10 recursive function in your favourite programming language.
Use only addition.
Examples:
multiplyby10 (5, 1) = 50
multiplyby10 (23, 3) = 23000
multiply :: Int -> Int -> Int
multiply n 0 = n
multiply n p = read (prefix ++ suffix)
where prefix = show n
suffix = replicate p '0'
>>54906644fn mul(a : u64, b : i32, acc : u64) -> u64 {
if b == 0 {
acc
} else {
mul(a, b - 1, acc + a)
}
}
fn e10(b : u64, e : i32) -> u64 {
if e > 0 {
e10(mul(b, 10, 0), e - 1)
} else {
b
}
}
>>54907000
got damn rust is ugly
>>54906779
Just realised, I don't need the first clause, if I'm not doing recursion.
r8double MultiplyByTen(double number, int index)
{
if (index == 0) return number;
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));
double originalValue = number;
for (int i = 0; i < 9; i++)
{
number += originalValue;
}
return MultiplyByTen(number, index - 1);
}
>>54906644#!/bin/bash
function multiplyBy10() {
# check if both args are integers
intTest='^[0-9]+$'
if [[ ${1} =~ $intTest ]] && [[ ${2} =~ $intTest ]]
then
printf '%s' ${1}
printf '0%.0s' {1..${2}}
fi
}
>>54907240
I think this would be enough.for (int i = 0; i < 9; i++) number += originalValue;
>>54907353
That's the same thing. If you wanna code golf you can just reduce it todouble m(double n, int i)
{
if (i<=0) return n;
double o=n,k=0;
for (;k++<9;)n+=o;
return m(n, i-1);
}
function powers(x, n) {
return eval(x + "e" + n)
}
>>54907728
Kekimus maximus
int Multiply10(int n, int x)
{
string number = itoa(n);
char zero='0';
for(int i =0;i<x;i++)
string. append(zero) ;
return atoi(number. c_str());
}
>>54908461
crap, should be number.append(zero);
>>54908461
disgusting
>>54906644
Recursivefunction mb10(a, b) {
return (b < 1) ? a : mb10(a*10, b-1)
}
What I would actually dofunction mb10(a, b) {
return parseInt(`${a}e${b}`)
}
>>54908798
>Use only addition