Skip to main content
added 215 characters in body
Source Link
Rojo
  • 43.1k
  • 7
  • 100
  • 196

This answer should be read upside down, since the last edit has the fastest, neatest and shortest answer

Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 

EDIT2

Slightly faster but I like it twice as much

happyQ[1] = True; e : happyQ[i_] := (e = False; e = happyQ[#.# &@IntegerDigits[i]]) 

or perhaps to make it slightly shorter and a little bit more memory efficient, reducing the recursion tree's height

happyQ[1] = True; e : happyQ[i_] := e = happyQ[e = False; #.# &@IntegerDigits[i]] 

This answer should be read upside down, since the last edit has the fastest, neatest and shortest answer

Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 

EDIT2

Slightly faster but I like it twice as much

happyQ[1] = True; e : happyQ[i_] := (e = False; e = happyQ[#.# &@IntegerDigits[i]]) 

This answer should be read upside down, since the last edit has the fastest, neatest and shortest answer

Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 

EDIT2

Slightly faster but I like it twice as much

happyQ[1] = True; e : happyQ[i_] := (e = False; e = happyQ[#.# &@IntegerDigits[i]]) 

or perhaps to make it slightly shorter and a little bit more memory efficient, reducing the recursion tree's height

happyQ[1] = True; e : happyQ[i_] := e = happyQ[e = False; #.# &@IntegerDigits[i]] 
added 154 characters in body; added 108 characters in body
Source Link
Rojo
  • 43.1k
  • 7
  • 100
  • 196

This answer should be read upside down, since the last edit has the fastest, neatest and shortest answer

Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 

EDIT2

Slightly faster but I like it twice as much

happyQ[1] = True; e : happyQ[i_] := (e = False; e = happyQ[#.# &@IntegerDigits[i]]) 
Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 

This answer should be read upside down, since the last edit has the fastest, neatest and shortest answer

Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 

EDIT2

Slightly faster but I like it twice as much

happyQ[1] = True; e : happyQ[i_] := (e = False; e = happyQ[#.# &@IntegerDigits[i]]) 
deleted 2 characters in body
Source Link
Rojo
  • 43.1k
  • 7
  • 100
  • 196
Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]^2]#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 
Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]^2]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 
Module[{$guard = True}, happyQ[i_] /; $guard := Block[{$guard = False, appeared}, appeared[_] = False; happyQ[i] ] ] e : happyQ[_?appeared] := e = False; happyQ[1] = True; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.#&@IntegerDigits[i]]) 

Now, taking this from @LeonidShiffrin

happyPrimeN[n_] := Module[{m = 0, pctr = 0}, While[m < n, If[happyQ@Prime[++pctr], m++]]; {pctr, Prime[pctr]}]; 

EDIT

Ok, this was cool, but if you don't mind wasting a little memory and not resetting appeared, it becomes simple and less cool

appeared[_] = False; happyQ[1] = True; happyQ[_?appeared] = False; e : happyQ[i_] := e = (appeared[i] = True; happyQ[#.# &@IntegerDigits[i]]) 
deleted 1 characters in body
Source Link
Rojo
  • 43.1k
  • 7
  • 100
  • 196
Loading
Post Made Community Wiki by Mr.Wizard
added 340 characters in body
Source Link
Rojo
  • 43.1k
  • 7
  • 100
  • 196
Loading
added 21 characters in body
Source Link
Rojo
  • 43.1k
  • 7
  • 100
  • 196
Loading
Source Link
Rojo
  • 43.1k
  • 7
  • 100
  • 196
Loading