Message Boards Message Boards

1
|
10129 Views
|
7 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Make a table of the first digit of the first 20 squares

Posted 5 years ago

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.

POSTED BY: bob tan
7 Replies
Table[Column[{n, n^2, n^3}], {n, 20}]

That´s the answer

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
POSTED BY: Murray Eisenberg

Perhaps nicer: take a look at attached notebook

Attachments:
POSTED BY: Tomas Garza

Identical but extended with //Dataset or //Framed or //Dataset//Framed

TableForm[Table[{n, n^2, n^3}, {n, 20}], 
  TableHeadings -> {None, {"n", "n^2", "n^3"}}
  , TableAlignments -> {Right, Center}
  , TableSpacing -> {1.5, 2}
  ] // Framed

enter image description here

POSTED BY: Jos Klaps

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.

POSTED BY: Hans Michel
Posted 5 years ago

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

POSTED BY: bob tan

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}*)
POSTED BY: Hans Michel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract