Group Abstract Group Abstract

Message Boards Message Boards

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

Finding Happy/Sad Numbers

Anonymous User
Anonymous User
Posted 11 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 11 years ago

Also, you can replace

ToExpression@Characters[ToString@x]

with

IntegerDigits[x]
POSTED BY: Eric Rimbey
Anonymous User
Anonymous User
Posted 11 years ago

Thank you so much for your explanation, it really help me a lot!

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

You are welcome

POSTED BY: Marian Muresan
Anonymous User
Anonymous User
Posted 11 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
Anonymous User
Anonymous User
Posted 11 years ago

Thank you for your suggestion :)

POSTED BY: Anonymous User

On your topic I suggest you the book HAZRAT, R., Mathematica: A Problem-Centered Approach, Springer, 2010, Springer Undergraduate Mathematics Series, London, xv+188.

Marian

POSTED BY: Marian Muresan

Every number eventually returns a 1 or 4 in the ones place. I think what you're trying to do is count how many iterations it takes before you get there. If so, here's the answer:

y = 0; Do[y = x; i = 0; 
 While[y != 1 && y != 4, i++; 
  y = Total[Power[#, 2] & /@ ToExpression@Characters[ToString@y]]];
 Print[{x,i, y}], {x, 99}]
POSTED BY: Eric Smith
Anonymous User
Anonymous User
Posted 11 years ago

Thank you so much Eric for your help. I've also just seen that the numbers 3 etc. don't work because they get higher than 100. I've also got another equation, but I'll try yours as well.

Have a good one,

POSTED BY: Anonymous User

Can I recommend using this as your function then:

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

If your output goes above 100, your equation doesn't work and things spin out of control.

POSTED BY: Eric Smith
Anonymous User
Anonymous User
Posted 11 years ago
  1. I thought, the test is "x != 1 && x != 4": when it is true, it calculates one time, then "x" gets tested again, or otherwise it stops immediately and gives (like the numbers 1 and 4) the result.

  2. I didn't want to end the calculations with a certain number of calculations ("i"). The numbers shouldn't get too high, especially because we're just using 1-digit numbers...

I've already said the biggest issue: It doesn't make sense at all that the numbers get really high like the numbers 5 and 7.

Here's an example (this is how the algorithm should work): Number 3: 3^2=9 -> 9^2= 81 -> 8^2+1^2=65 ... Cycle: 3-9-81-65-61-37-58-89-145-42-20-4

POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 11 years ago

Thanks, I haven't noticed that; I should open my eyes :)

POSTED BY: Anonymous User
POSTED BY: Eric Smith
POSTED BY: Eric Smith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard