[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Write your multiplying by powers of 10 recursive function in

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 3

File: 12-1024x724.png (300KB, 1024x724px) Image search: [Google]
12-1024x724.png
300KB, 1024x724px
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'
>>
>>54906644
fn 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.
>>
r8

double 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 to

double 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);
}
>>
File: 1396927874103.jpg (155KB, 1031x882px) Image search: [Google]
1396927874103.jpg
155KB, 1031x882px
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

Recursive
function mb10(a, b) {
return (b < 1) ? a : mb10(a*10, b-1)
}


What I would actually do
function mb10(a, b) {
return parseInt(`${a}e${b}`)
}
>>
File: 1405808156512.jpg (139KB, 600x404px) Image search: [Google]
1405808156512.jpg
139KB, 600x404px
>>54908798
>Use only addition
Thread posts: 16
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.