Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.1K Views
|
14 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Finding Happy/Sad Numbers

Anonymous User
Anonymous User
Posted 10 years ago

I'm trying to find "happy / sad numbers", that means you take the squares of the digits of your number and add them together, then you repeat this over and over again; at the end, you'll either get 1 or a repeating cycle with 4. I also want to know, how many times you have to do this to get to the number 1 or into the cycle 4 (in my code, I just want to get either the number 1 or 4 to end the calculations):

Do[For[i = 0, x != 1 && x != 4, x = ((x - mod[x, 10])/10)^2 + (mod[x, 10])^2, i++]; Print[{i, x}], {x, 99}]

My thoughts: I want to do this with x from 1 to 99 (the equation in the middle can only handle these numbers; I'll insert another one afterwards). "i" is here to count the number of calculations. In the For-loop is the test whether x is 1 or 4, so it has to stop, or x is allowed to get a new value. When it leaves, it prints out the number of calculations and the end-number. But the output looks like this:

{0,1}
{1,1/100 (2-mod[2,10])^2+mod[2,10]^2}
{1,1/100 (3-mod[3,10])^2+mod[3,10]^2}
{0,4}
{1,1/100 (5-mod[5,10])^2+mod[5,10]^2}

etc.

I'm not familiar with the Wolfram Language because I am really new here. I would be pleased if you could please tell me why this happend and how I could prevent such mistakes.

POSTED BY: Anonymous User
14 Replies
Posted 10 years ago
POSTED BY: Eric Rimbey

You are welcome

POSTED BY: Marian Muresan
POSTED BY: Marian Muresan
Anonymous User
Anonymous User
Posted 10 years ago
POSTED BY: Anonymous User
POSTED BY: Eric Smith
Anonymous User
Anonymous User
Posted 10 years ago

(I've just seen that I've somehow copied your second latest comment before, I don't know what happend but sorry)

Yes, you've exactly done what I've wanted to do, but much more elegant (I've wanted to get there step by step; apparently I have to learn many things).

Can you please tell me how to understand your answer? It's something like: you take the total of every squared digit of "y" and put it back to a string, right?

POSTED BY: Anonymous User

Sorry for the delay, hopefully this is still helpful...

Take your number apart by converting it to a string and putting each character (digit) into a list. Look up the documentation for Characters if you're not sure what it does.

Characters[ToString@x]

If the ToString@x notation confuses you it's the same as ToString[x]. Now your list is still a list of strings and you can't add or square strings. So turn the list into an expression

ToExpression@Characters[ToString@x]

You want to square each element so make a Pure Function with the Power function: Power[#,2]&. Then take this function and Map it to each element of the array using /@

Power[#,2]&/@ToExpression@Characters[ToString@x]

That about does it. Just wrap the expression with Total to add up all the elements.

A couple other comments,

  1. I switched to While because that's the logical test you're doing. You can use For but it's kind of clumsy.
  2. You don't want to alter x. You want x to stay the number between 1 and 99 until you've converged to 1 or 4. So to "keep it safe", set an intermediate variable y equal to x and do your operations on it. Also increment i so you see how many times it takes to satisfy your while loop.
POSTED BY: Eric Smith
Anonymous User
Anonymous User
Posted 10 years ago
POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 10 years ago
POSTED BY: Anonymous User
POSTED BY: Eric Smith
Anonymous User
Anonymous User
Posted 10 years ago
POSTED BY: Anonymous User
POSTED BY: Eric Smith
POSTED BY: Eric Smith
Anonymous User
Anonymous User
Posted 10 years ago
POSTED BY: Anonymous User
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard