Skip to main content
added 2 characters in body
Source Link
george2079
  • 39.3k
  • 1
  • 45
  • 115

The idea here is among all the tuples with the same digits only the one sorted lowest needs to be checked. That sort/union operation is much faster than the total^2 operation so it is worthwhile:

 SumLimit = 450 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

{0.608404, 799999}

Faster still, here we avoid generating all the tuples to begin with.

 nd = Ceiling[SumLimit/81];81]+1; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.140401, 799999}

The idea here is among all the tuples with the same digits only the one sorted lowest needs to be checked. That sort/union operation is much faster than the total^2 operation so it is worthwhile:

 SumLimit = 450 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

{0.608404, 799999}

Faster still, here we avoid generating all the tuples to begin with.

 nd = Ceiling[SumLimit/81]; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.140401, 799999}

The idea here is among all the tuples with the same digits only the one sorted lowest needs to be checked. That sort/union operation is much faster than the total^2 operation so it is worthwhile:

 SumLimit = 450 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

{0.608404, 799999}

Faster still, here we avoid generating all the tuples to begin with.

 nd = Ceiling[SumLimit/81]+1; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.140401, 799999}

added 260 characters in body
Source Link
george2079
  • 39.3k
  • 1
  • 45
  • 115

The idea here is among all the tuples with the same digits only the one sorted lowest needs to be checked. That sort/union operation is much faster than the total^2 operation so it is worthwhile:

 SumLimit = 500450 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

4999999{0.608404, 799999}

Faster. still, here we avoid generating all the tuples to begin with.

 nd = Ceiling[SumLimit/81]; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.312002, 4999999}

{0.140401, 799999}

 SumLimit = 500 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

4999999

Faster..

 nd = Ceiling[SumLimit/81]; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.312002, 4999999}

The idea here is among all the tuples with the same digits only the one sorted lowest needs to be checked. That sort/union operation is much faster than the total^2 operation so it is worthwhile:

 SumLimit = 450 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

{0.608404, 799999}

Faster still, here we avoid generating all the tuples to begin with.

 nd = Ceiling[SumLimit/81]; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.140401, 799999}

added 524 characters in body
Source Link
george2079
  • 39.3k
  • 1
  • 45
  • 115
 SumLimit = 500 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

4999999

Faster..

 nd = Ceiling[SumLimit/81]; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.312002, 4999999}

 SumLimit = 500 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

4999999

 SumLimit = 500 First@(FromDigits /@ Select[Union@(Sort /@ Tuples[Range[0, 9], {Ceiling[SumLimit/81]}]), Total@(#^2) > SumLimit &]) 

4999999

Faster..

 nd = Ceiling[SumLimit/81]; First@Sort[ FromDigits /@ Select[ #[[2]] & /@ Nest[( Flatten[ Map[(next = #[[1]] + 1; {next, #} & /@ ({#[[2]]}~Join~ Table[ Join[#[[2]] , ConstantArray[#[[1]], i ] ], {i, 1, nd - Length[#[[2]]]}])) & , #, 1], 1]) & , {{0, {}}} , 10] , Length[#] == nd && Total[#^2] > SumLimit&]] // Timing 

{0.312002, 4999999}

Post Undeleted by george2079
added 52 characters in body
Source Link
george2079
  • 39.3k
  • 1
  • 45
  • 115
Loading
Post Deleted by george2079
Source Link
george2079
  • 39.3k
  • 1
  • 45
  • 115
Loading