Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.5K 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

A couple other things. These numbers are going to get really big really fast. I didn't look closely at your algorithm, just the syntax, and there are a couple issues.

  1. Your For loop won't ever exit if you don't have a test to escape when i gets too large and x doesn't satisfy your conditions.
  2. You're not incrementing i in the correct place. Your increment is the third argument, not the 4th.

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

I've decreased the range of i and x to 9 and 10. You'll probably want to skip the print statement if you've hit the upper bound of i that you set i.e. If[i<10,Print[{i,x}]. Even with this very truncated version the numbers get large fast:

{0,1}
{1,4}
{10,485}
{0,4}
{10,29052125}
{10,145}
{10,7341998783795942461}
{10,53905}
{10,2329}
{1,1}
POSTED BY: Eric Smith

Did you mean to use Mod instead of mod. Remember all built in functions in the Wolfram Language are capitalized. When it sees mod and there's nothing in the lookup table, then it returns it as a generic head of your expression.

POSTED BY: Eric Smith
Posted 10 years ago

Also, you can replace

ToExpression@Characters[ToString@x]

with

IntegerDigits[x]
POSTED BY: Eric Rimbey
Anonymous User
Anonymous User
Posted 10 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 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
Anonymous User
Anonymous User
Posted 10 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 10 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 10 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 10 years ago

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

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