-3

how to write a function to solve:

For eg. if number is 4237, solution

yields 4+2 2+3 3+7 
         6   5  10
then   6+5 5+10    yields
        11   15

1115 is the answer not row vector [11,15].

while or for should not be used.

Edited. Eg 2. If initial number is 21020, final answer should be 77. Should be number 77 not vector [7,7].

timrow
  • 65
  • 2
  • 7

5 Answers5

2

Solution without loop:

%// input
a = 21020

%// convert number to array of digits
b = num2str(a)-48
%// or directly 
b = [2 1 0 2 0]

%// get antidiagonal of pascal matrix
v = diag(fliplr(pascal(numel(b)-1))).'

%// calculation
c = sum([b(1:end-1).*v; b(2:end).*v],2)

%// convert array of digits to number inspired by Luis Mendo
result = str2double(sprintf('%i',c))

Solution with loop:

%// calculation
for ii = 1:numel(b)-2; b = filter(ones(1,2),1,b); end

%// convert array of digits to number inspired by Luis Mendo
result = str2double(sprintf('%i',b(end-1:end)))

result =

     77
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
1

I think this does what you want. It does use a loop.

x = 4237; %// input
x = dec2base(x,10)-'0';
for n = 1:numel(x)-2
    x = conv(x,[1 1],'valid');
end
y = str2num(sprintf('%i',x)); %// output

Without loops: you can convolve [1 1] with itself multiple times, and then convolve that once with your digits. The N-fold convolution of [1 1] with itself is just binomial coefficients, which can be computed easily with the gammaln function:

x = 4237; %// input
x = dec2base(x,10)-'0';
N = numel(x)-2;
coeffs = round(exp(gammaln(N+1)-gammaln(1:N+1)-gammaln(N+1:-1:1)));
x = conv(x,coeffs,'valid');
y = str2num(sprintf('%i',x)); %// output
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • I prefer `conv` over`filter` as it shortens the vector automatically. +1 – Robert Seifert Feb 13 '15 at 10:31
  • Correction. I made a mistake. The final answer should be 77 if initial number is 21020. – timrow Feb 13 '15 at 13:24
  • Your `conv` solution is perfect. In order to escape using loops, I used this principle with recursion. Recursion is technically not a loop and I wrote an answer. Thanks for allowing me to flex my muscles with recursion! – rayryeng Feb 13 '15 at 19:20
  • @rayryeng Recursion is always hard. At least I find it much more difficult than loops – Luis Mendo Feb 13 '15 at 21:22
  • @ray BTW, do you want to try your luck on [this one](http://stackoverflow.com/q/28501418/2586922)? :-) – Luis Mendo Feb 13 '15 at 21:25
  • @LuisMendo - I will admit that recursion can certainly be hard to understand, but after studying some functional programming (Standard ML, Scala, Racket), I got very comfortable with it. It just requires practice! – rayryeng Feb 13 '15 at 21:45
  • @LuisMendo hahahah! I've never heard that one before! – rayryeng Feb 13 '15 at 21:52
1

Simply for academic purposes, we can take a look at using recursion combined with conv. This is inspired by both Luis Mendo's and Amit's approach.

In other words:

function [final] = convertNum(x)    

    function [out] = helper(in)

        if numel(in) == 2
            out = in;
        else
            out = helper(conv(in, [1 1], 'valid'));
        end
    end
    digits = dec2base(x, 10) - '0';
    final_digits = helper(digits);
    final = str2num(sprintf('%i',final_digits));

end

convertNum is the function we will use to take in a number and the output will be a two element vector that produces the sum of pair-wise elements at each step until there are two elements left.

We need a helper function that will take in an array of coefficients where this array consists of the extracted individual digits of the input number into convertNum, which is stored in x. What we do first is take our number x and convert the digits into individual numbers (taken from thewaywewalk, Luis Mendo and Amit). Next, we call the helper function to compute our pair-wise sum.

The helper function operates in such a way where if we have an input number whose length is not equal to 2, we perform the pair-wise sum via conv and use this to recurse into our helper function. Once the input consists of only two elements, this is what we return from the helper function, we take these two vectors and combine them into a single number. This is what we finally return to the user.

As such, working with x = 21020, we get:

final = convertNum(21020)

final = 

77

Similarly:

final = convertNum(4237)

final = 

1115
rayryeng
  • 102,964
  • 22
  • 184
  • 193
0

Based on your comments it seems that your number is already in a vector form. thus you were close. You were only missing a loop like, for example, this one:

x = [4,2,3,7];

while numel(x) > 2
    x = x(1:end-1) + x(2:end);
end

The result is:

x =

    11    15
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • It gives a wrong answer. For instance, if x= 21020, the final answer should be 1115 not 3122. – timrow Feb 13 '15 at 07:58
  • This gives the right answer but does not describe the entire picture. The only thing left would be to turn `x` back into a single number. Given that `x` is a vector of two elements, the job would be to concatenate these both into a single number. – rayryeng Feb 13 '15 at 21:47
0

Using answers by Luis Mendo and Marcin, the given solution can be implemented using recursive function as follows(without using for or while):

function x=reduce(x)
  x = dec2base(x,10)-'0'; 
  x=calculate(x);
end
function y=calculate(z)
  if(numel(z)>2)
    y=calculate(z(1:end-1)+z(2:end));
  else
    y=z;
  end
end
lonstud
  • 525
  • 2
  • 19