0
$\begingroup$

I have the following code for getting smooth numbers up to Prime[4] :

g = 100;

t1 = Table[{2^x*3^y*5^z*7^u}, {u, 0, Log[7, g]}, {z, 0, Log[5, g/(7^u)]}, {y, 0, Log[3,g/(7^u*5^z)]}, {x, 0, Log[2, g/(7^u*5^z*3^y)]}]
t2 = Sort[Flatten[t1]]

I am looking for a general solution for k Primenumbers (in ascending order) using Table which refers to iterators appearing in the loop before. I know I can use FactorInteger and Select or fast code from (Creating a Table with varying depth and interdependent limits) to solve this problem, but how to tackle it using Table? Is there a way to do this? I think the answer lies somewhere in Table with dependent iterator but here are too many tiwsts for my level of understanding.

$\endgroup$
3

1 Answer 1

2
$\begingroup$
Clear["Global`*"]

g = 100;

t1 = Table[{2^x*3^y*5^z*7^u}, {u, 0, Log[7, g]}, {z, 0, Log[5, g/(7^u)]}, {y, 
    0, Log[3, g/(7^u*5^z)]}, {x, 0, Log[2, g/(7^u*5^z*3^y)]}];

t2 = Sort[Flatten[t1]]

(* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, \
30, 32, 35, 36, 40, 42, 45, 48, 49, 50, 54, 56, 60, 63, 64, 70, 72, 75, 80, \
81, 84, 90, 96, 98, 100} *)

Generalizing to n primes

smooth[n_Integer?Positive, g_Integer?Positive] :=
 Module[{a, func, factors, iterators,
   primes = Prime /@ Range[n],
   vars = Array[a, n]},
  func = Times @@ (primes^vars);
  factors = #[[1]]^#[[2]] & /@ Transpose[{primes, vars}];
  iterators = {#[[1]], 0, Log[#[[2]], #[[3]]]} & /@
    Transpose[{
      Reverse@vars,
      Reverse@primes,
      g/FoldList[Times, 1, Most@Reverse@factors]}];
  Table[func, Evaluate[Sequence @@ iterators]] // Flatten // Sort // Quiet]

Checking against original calculations

smooth[4, g] == t2

(* True *)
$\endgroup$
3
  • $\begingroup$ Wow! You put all the puzzle parts together. Like a piece of art! Great! The only part I still have to struggle with is Evaluate Sequence. I read the manual but it is still not very clear to me. $\endgroup$
    – user57467
    Commented Aug 27, 2020 at 10:13
  • 1
    $\begingroup$ Table requires a sequence of one or more iterators. iterators is a list of the iterators. Sequence @@ iterators would convert the list into a sequence. However, since Table has the attribute HoldAll, the conversion would not be done when it needs to be. Evaluate forces early evaluation of this conversion so that Table sees the required sequence. $\endgroup$
    – Bob Hanlon
    Commented Aug 27, 2020 at 14:46
  • $\begingroup$ Thank you for clearing things up! $\endgroup$
    – user57467
    Commented Aug 27, 2020 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.