[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Write your multiplying by powers of 10 recursive function in
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 16
Thread images: 3
File: 12-1024x724.png (300 KB, 1024x724) Image search: [Google]
12-1024x724.png
300 KB, 1024x724
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 (155 KB, 1031x882) Image search: [Google]
1396927874103.jpg
155 KB, 1031x882
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 (139 KB, 600x404) Image search: [Google]
1405808156512.jpg
139 KB, 600x404
>>54908798
>Use only addition
Thread replies: 16
Thread images: 3

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.