Problem 2

Question:

Display the first 20 powers of 2,i.e.{2,4,8,...}.

Answer:

(a)

There are several ways to do this.  Simplest is to use lists, using the fact that 2^{1, 2, 3} = {2, 4, 8}, and Range gives you a list of integers.

2^Range[20]

{2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576}

(b)

Another way is with a looping construction.  We haven't talked about loop constructions yet, but they're coming up soon - and I saw that several people had tried to use them for this problem.  This one generates a list by evaluating the 2^ifor i=1 to 20.

Table[2^i, {i, 1, 20}]

{2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576}

(c)

And finally here's one that loops from i=1 to 20, printing out each 2^i.  
The downside of this kind of construction is that printing the result doesn't save it for another round of manipulation.

Do[Print[2^i], {i, 1, 20}]

2

4

8

16

32

64

128

256

512

1024

2048

4096

8192

16384

32768

65536

131072

262144

524288

1048576


Created by Mathematica  (September 13, 2004)