Group Abstract Group Abstract

Message Boards Message Boards

1
|
13.7K Views
|
7 Replies
|
1 Total Like
View groups...
Share
Share this post:

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

Posted 6 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
POSTED BY: Hans Michel
Posted 6 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