Try out this test problem first.

Given the variable x as your input, multiply it by two and put the result in y.

Examples:

Input x = 2 Output y is 4

Input x = 17 Output y is 34

**Press the "Solve This Now!" button below to get started...**

Show
12 older comments

Sam Nazari
on 9 Apr 2012

Why does y = bitshift(x,1); give a larger solution size than the most common answer listed so far?

Manuel Ignacio
on 6 Feb 2013

The first problem. Very easy but enough to start the Cody Game. Great game!

Adrian Dronca
on 7 Mar 2013

@Sam Nazari
I guess it`s because calling bitshift makes the code size bigger. You call a function.
When there is only x*2 its 1 operation only.

JeremyR
on 20 Jun 2013

Deceivingly easy start to the game!

raviraja
on 11 Dec 2013

Ha ha I got it

Ruchi Agarwal
on 13 Feb 2014

Great!

Matthew Brown
on 16 Dec 2014

I did in fact start here.

Alan Martinez
on 23 Dec 2014

Good problem for beginners.

Prita Vaidya
on 30 Nov 2015

Nice!!!

Pedro Pascual González
on 30 Nov 2015

Yeah

Brendan Hofmann
on 8 Dec 2015

cool

Acylos
on 5 Jan 2016

alright

Roberto
on 5 Mar 2016

I tried to use bitshift but I forgot it doesn't work with x<0

Daniel E. Loza Morales
on 12 May 2016

Easy starter problem. Except I was expecting Cody, to require a '.m' file upload.

Mashharar Bhuiyan
on 28 Aug 2016

very basic,very interesting indeed

1 Comment

Alo
on 12 Mar 2016

Nice

1 player likes this solution

1 Comment

Abdullah Momoh
on 9 Jan 2016

Addition is used instead of multiplication as it will take a shorter amount of time to compute

1 Comment

Matthew Atilano
on 6 Dec 2015

It might help to put in the variable x.

1 Comment

Daniel Jenkinson
on 4 Dec 2015

Is there a way to make y autoupdate when you change the value of x?

1 Comment

Sierra McGee
on 9 Nov 2015

This is a basic solution, everyone should be able to solve this.

1 player likes this solution

2 Comments

Ruth tannert
on 30 Sep 2015

i did it!!!

Jakub Mrowka
on 19 Sep 2016 at 8:02

gz

1 Comment

Matt Brennan
on 7 Sep 2015

Step 1 complete

1 Comment

korraprolu brahma
on 3 Jun 2015

trying the best

1 Comment

Alexander Wickstrom
on 28 May 2015

Is it necessary to use the . after 2?
Couldn't you write y = 2*x; ?

1 Comment

Sebastian Cuenca
on 26 Apr 2015

Yeah, i do it

1 Comment

Matthew Caron
on 7 Jan 2015

This was a great starter question.

1 Comment

asdasdasd
on 26 Dec 2014

what is the correct answer??

1 Comment

Divya
on 16 Jan 2014

Is it wrong?

2 Comments

Anand Abhishek Singh
on 24 Sep 2013

how leading solution size is 0?

Clayton
on 11 Nov 2013

Probably hacked something

6 players like this solution

9 Comments

Show
6 older comments

Greg
on 9 Sep 2013

What the heck is freepass?

Ramesh Vukyam
on 23 Oct 2013

Is this a solution ?

J.R.! Menzinger
on 10 Feb 2014

Nice place to test... ;-)

Alison
on 20 May 2014

cute but ridiculous

213
on 28 Jun 2014

why it's 'freepass.please',not other words else?

Alfonso Nieto-Castanon
on 28 Jun 2014

'freepass' is just a (cute but ridiculous) hack (see problem http://www.mathworks.com/matlabcentral/cody/problems/1774-free-passes-for-everyone ). The 'please' part is because we are really polite...

Pinakin
on 20 Apr 2015

hello Alfonso !
I'd like to know how the solution size for your solution to the 'times two' question is '0'
can you please explain ?
thanks !

Erenca
on 29 Oct 2015

how is this even possible, solution with 0 size?

Dan Po
on 24 Sep 2016 at 0:53

does freepass; still work?

1 player likes this solution

2 Comments

ABHAY SONI
on 7 Dec 2012

can anyone tell me why this is incorrect

Ned Gulley
on 12 Mar 2013

I see two problems. It needs to be a function (as modeled by the template), and the input command hangs and times out when called by a web service.

8 players like this solution

5 Comments

Show
2 older comments

Dirk Engel
on 27 Jun 2012

It works, but it's ugly :-)

Chang Ho
on 6 Aug 2012

Nice! :)

Marlies
on 16 Nov 2012

Never realized that MATLAB sees a defined assignment (y = 2*x) different than no assignment (2*x => assignment to ans).
If anything, I would have guessed the second would be (slightly) more effort.

Zaka
on 23 Mar 2013

does this variable "ans" overrides MATLAB's default "ans"?

David Amor
on 27 Apr 2013

ok, i got there in the end. but it defeats the rules - there is no "y" variable when you change the template to ans = times2(x)...
how do you know what the rules are?!?

2 players like this solution

1 Comment

Justyna Zander
on 3 Apr 2012

Missing test cases:
assert(isequal(times2(0),0));
assert(isequal(times2(0.1),0.2));
assert(isequal(times2(0.000001),0.000002));
assert(isequal(times2(0.1000000000001),0.2000000000002));
assert(isequal(times2(1.0000000000000000000000000000001),2));
assert(isequal(times2(-0),0));
... and a few others.

1 player likes this solution

2 Comments

Jan Simon
on 26 Jan 2012

The test suite is weak. Please add:
assert(isequal(times2(Inf), Inf))
assert(isequal(times2(NaN), NaN))
assert(isequal(times2([]), []))
assert(isequal(times2(magic(3)), 2*magic(3)))

Jan Simon
on 26 Jan 2012

To focus the power of Matlab, add these tests:
assert(isequal(times2(Inf), Inf)) assert(isequal(times2(NaN), NaN))
assert(isequal(times2([]), [])) assert(isequal(times2(magic(3)), 2*magic(3)))

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
60 players like this problem

60 players like this problem