Group Abstract Group Abstract

Message Boards Message Boards

1
|
17.4K Views
|
18 Replies
|
9 Total Likes
View groups...
Share
Share this post:

[EIWL] Solving Example 6.10 of the Stephen Wolfram book

Posted 9 years ago

Hi all.

I am working my way through the exercises on this page:

https://www.wolfram.com/language/elementary-introduction/06-making-tables.html

However I am stuck on Q. 6.10: Make a list line plot of the first digits of the first 100 squares.

IntegerDigits[Range[100]^2] 

results in:

    {{1},{4},{9},{1,6},{2,5},{3,6},{4,9},{6,4},{8,1},{1,0,0},{1,2,1},{1,4,4},{1,6,9},{1,9,6},{2,2,5},{2,5,6},{2,8,9},{3,2,4},{3,6,1},{4,0,0},{4,4,1},{4,8,4},{5,2,9},

{5,7,6},{6,2,5},{6,7,6},{7,2,9},{7,8,4},{8,4,1},{9,0,0},{9,6,1},{1,0,2,4},{1,0,8,9},{1,1,5,6},{1,2,2,5},{1,2,9,6},{1,3,6,9},{1,4,4,4},{1,5,2,1},{1,6,0,0},{1,6,8,1},

{1,7,6,4},{1,8,4,9},{1,9,3,6},{2,0,2,5},{2,1,1,6},{2,2,0,9},{2,3,0,4},{2,4,0,1},{2,5,0,0},{2,6,0,1},{2,7,0,4},{2,8,0,9},{2,9,1,6},{3,0,2,5},{3,1,3,6},{3,2,4,9},

{3,3,6,4},{3,4,8,1},{3,6,0,0},{3,7,2,1},{3,8,4,4},{3,9,6,9},{4,0,9,6},{4,2,2,5},{4,3,5,6},{4,4,8,9},{4,6,2,4},{4,7,6,1},{4,9,0,0},{5,0,4,1},{5,1,8,4},{5,3,2,9},

{5,4,7,6},{5,6,2,5},{5,7,7,6},{5,9,2,9},{6,0,8,4},{6,2,4,1},{6,4,0,0},{6,5,6,1},{6,7,2,4},{6,8,8,9},{7,0,5,6},{7,2,2,5},{7,3,9,6},{7,5,6,9},{7,7,4,4},{7,9,2,1},

{8,1,0,0},{8,2,8,1},{8,4,6,4},{8,6,4,9},{8,8,3,6},{9,0,2,5},{9,2,1,6},{9,4,0,9},{9,6,0,4},{9,8,0,1},{1,0,0,0,0}}

Now I would like to count the length of individual elements in the above list ie. {1,1,1,2,2,2,2,2,2,3,3,3....}. But how?

Thanks in advance.

D

EDIT: This is not homework. I am self-learning Mathematica, and will be doing all the exercises in all 47 Chapters, which will probably take me a few weeks to a few months. Hence you may see me asking lots of questions in this forum! :)

POSTED BY: D P
18 Replies
POSTED BY: Arno Bosse

About Exercise 6.8:

Make a list line plot of the number of digits in each of the first 100 squares.

One of the possible solutions:

ListLinePlot[Table[Length[IntegerDigits[i^2]], {i, 1, 100}]]

enter image description here

Another solution:

ListLinePlot[
 Table[Length[IntegerDigits[Range[100]^2][[i]]], {i, 100}]]

enter image description here

I would use Table and Length like this:

list = IntegerDigits[Range[100]^2];
Table[Length[list[[i]]], {i, 1, Length[list]}]

I'm sure that there are other ways, but this one is pretty straightforward.

POSTED BY: Tim Mayes
Posted 9 years ago

Hint (and only a hint):

You have a list of items and you want to do the same thing to every one of those items.

Think "Map" and then think what is that thing that you want to do to every one of those individual items (which just happen to be lists).

POSTED BY: Bill Simpson

Thank you, I was also very stuck on this one.

POSTED BY: Camille Driscoll

About the notation @. It is so named prefix form, i.e. you may use notation f[x] or f@x. They are identical.

For example:

Plot[Sin[x], {x, -7, 7}]

is identical to:

Plot[Sin@x, {x, -7, 7}],

i.e. Sin[x] and **Sin@x** are identical.

About the notation /@. It stands for the function Map[].

For example, you can write:

In[1]:= Map[f, {a, b, c, d, e}]

Out[1]= {f[a], f[b], f[c], f[d], f[e]}

    or in alternative input form:

In[2]:= f /@ {a, b, c, d, e}

Out[2]= {f[a], f[b], f[c], f[d], f[e]}

P.S. The first solution without the symbol @ is equivalent to

ListLinePlot[
 Table[First[Part[IntegerDigits[Table[i^2, {i, 100}]], j]], {j, 100}]]
Posted 9 years ago

Scroll down all the way in the left pane, past the last chapter and there is a link to all the solutions.

I wonder how many people gave up on EIWL because they couldn't solve the problems and didn't see the solutions buried at the bottom(off screen).

POSTED BY: D P
Posted 9 years ago

Thank you for in-depth answer above, however, I did not know syntax you used {embedded table formula and use of @ and in this example, this reply, /@. I found Arno Bosse's answer easier. (maybe because that was the solution I came upon first).

By the way, I am stuck at Question 8 of same chapter of the book. Any help?

POSTED BY: Jawad Mansoor
ListLinePlot@Table[IntegerDigits[i^2][[1]], {i, 100}]

Other variations on the same subject of Exercise 6.10:

ListLinePlot[IntegerDigits[Range[100]^2][[;; , 1]]]
IntegerDigits[Range[100]^2][[;; , 1]] // ListLinePlot    
ListLinePlot[First /@ IntegerDigits[Range[100]^2]]
First /@ IntegerDigits[Range[100]^2] // ListLinePlot
Table[IntegerDigits[i^2][[1]], {i, 100}] // ListLinePlot

From each row, select the first element.

That's interesting. I didn't understand at first how this could work, but

IntegerDigits[Range[100]^2] // TableForm

makes it clear. From all rows, select the first column.

POSTED BY: Arno Bosse

A solution without Table[]:

ListLinePlot[IntegerDigits[Range[100]^2][[All, 1]]]

Nice solution!

Posted 9 years ago

Thanks for the replies.

Apparently this is the solution, but the output is clearly not correct(according to the solution), so I need to make it work.

, 100}]]rst /

This is how I imagine the solution would be like, using functions that were taught in the 5 previous chapters of "An Elementary Introduction to the Wolfram Language".

Edit: I've already spent 2 days on this question, so it's time to move on, otherwise I will take a year or so to complete this online book. Maybe Stephen Wolfram(the author) expects too much of the reader, if he thinks that they can answer that question using only what has been taught previously. :)

POSTED BY: D P
Posted 9 years ago
POSTED BY: D P
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard