Skip to main content
Some speed tweaks, but still the same method
Source Link
wxffles
  • 14.4k
  • 1
  • 44
  • 75
Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := sos[k] = Plus @@ (#^2 & /@#.# IntegerDigits[k]);&[IntegerDigits[k]]; happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[1, history_List] := True; happyQ[k_Integer, history_List] := With[{h = sos[k]},  If[h == 1, True, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]];h]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{21.18750004531250, {12814, 137653}}

{0.0468750, {12814, 137653}}

(Edit: now even faster with more memoisation)

Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := sos[k] = Plus @@ (#^2 & /@ IntegerDigits[k]); happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[k_Integer, history_List] := With[{h = sos[k]},  If[h == 1, True, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{2.1875000, {12814, 137653}}

{0.0468750, {12814, 137653}}

(Edit: now even faster with more memoisation)

Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := sos[k] = #.# &[IntegerDigits[k]]; happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[1, history_List] := True; happyQ[k_Integer, history_List] := With[{h = sos[k]}, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{1.4531250, {12814, 137653}}

{0.0468750, {12814, 137653}}

Post Made Community Wiki by Mr.Wizard
More memoisation
Source Link
wxffles
  • 14.4k
  • 1
  • 44
  • 75
Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := sos[k] = Plus @@ (#^2 & /@ IntegerDigits[k]); happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[k_Integer, history_List] := With[{h = sos[k]}, If[h == 1, True, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{32.60937501875000, {12814, 137653}}

{0.0468750, {12814, 137653}}

(Edit: now even faster with more memoisation)

Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := Plus @@ (#^2 & /@ IntegerDigits[k]); happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[k_Integer, history_List] := With[{h = sos[k]}, If[h == 1, True, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{3.6093750, {12814, 137653}}

{0.0468750, {12814, 137653}}

Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := sos[k] = Plus @@ (#^2 & /@ IntegerDigits[k]); happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[k_Integer, history_List] := With[{h = sos[k]}, If[h == 1, True, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{2.1875000, {12814, 137653}}

{0.0468750, {12814, 137653}}

(Edit: now even faster with more memoisation)

added 552 characters in body
Source Link
wxffles
  • 14.4k
  • 1
  • 44
  • 75
Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := Plus @@ (#^2 & /@ IntegerDigits[k]); happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[k_Integer, history_List] := With[{h = sos[k]}, If[h == 1, True, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{3.6093750, {12814, 137653}}

{0.0468750, {12814, 137653}}

Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

Clear[happyPrimeN]; happyPrimeN[2000] = 137653; happyPrimeN[2000] // AbsoluteTiming 

{0., 137653}

But seriously, here's a memoised, recursive happyQ that can be used with Leonid's happyPrimeN

Clear[sos, happyQ]; sos[k_Integer] := Plus @@ (#^2 & /@ IntegerDigits[k]); happyQ[k_Integer] := happyQ[k] = happyQ[k, {}]; happyQ[k_Integer, history_List] := With[{h = sos[k]}, If[h == 1, True, If[MemberQ[history, h], False, happyQ[h, Prepend[history, h]]]]]; happyPrimeN[2000] // AbsoluteTiming happyPrimeN[2000] // AbsoluteTiming 

{3.6093750, {12814, 137653}}

{0.0468750, {12814, 137653}}

Source Link
wxffles
  • 14.4k
  • 1
  • 44
  • 75
Loading