Mathematica Asked on January 14, 2021
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 Prime
numbers (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.
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 *)
Correct answer by Bob Hanlon on January 14, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP