Hi all, beginner programmer here. I am having trouble with this exercise.
Make a table of the first digit of the first 20 square.
Can anyone help? Thank you.
Hi all, beginner programmer here. I am having trouble with this exercise.
Make a table of the first digit of the first 20 square.
Can anyone help? Thank you.
Not exactly sure is this what you looking for
Table[First[IntegerDigits[i^2]], {i, 20}]
(* {1, 4, 9, 1, 2, 3, 4, 6, 8, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4}*)
Yes Hans, thank you. How about:
Make a list for numbers n up to 20 in which each element is a column of the values of n, n^2 and n^3
There are many ways to do this in WL/Mathematica. Essence of your question is “take a list and display that list in a table-like structure”.
TableForm[Table[{n, n^2, n^3}, {n, 20}],
TableHeadings -> {None, {"n", "n^2", "n^3"}}]
(*
n n^2 n^3
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
11 121 1331
12 144 1728
13 169 2197
14 196 2744
15 225 3375
16 256 4096
17 289 4913
18 324 5832
19 361 6859
20 400 8000
*)
As a beginner to WL I would suggest that you review any of the beginner tutorials or the WL starter book.
Perhaps nicer: take a look at attached notebook
niceTable.nb (3.48 KB)
There’s no need for an iterator in many such situations, especially as in this one where you’re dealing with consecutive integers and individual functions (power-of-two and IntegerDigits) are listable. To obtain the desired first digits, you may simply do this:
First /@ IntegerDigits[Range[20]^2]
(* {1, 4, 9, 1, 2, 3, 4, 6, 8, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4} *)
If you want a table, you may use, for example:
{Range[20], First /@ IntegerDigits[Range[20]^2]} // TableForm
Table[Column[{n, n^2, n^3}], {n, 20}]
That´s the answer