Jaap Spies <j.spies <at> hccnet.nl> writes: > > > From Sloan's A001694: http://www.research.att.com/~njas/sequences/A001694 > > Powerful Numbers: > A positive integer n is powerful if for every prime p dividing n, p^2 also > divides n. > There is a Mathematica program. > > sage: mathematica_console() > Mathematica 5.2 for Linux x86 (64 bit) > Copyright 1988-2005 Wolfram Research, Inc. > -- Terminal graphics initialized -- > > In[1]:= Select[ Range[ 2, 2500 ], Position[ Union[ Transpose[ FactorInteger[ > # ] ][ [ 2 ] ] ], 1 ] == {} & ] > > Out[1]= {4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 72, 81, 100, 108, 121, 125, > > > 128, 144, 169, 196, 200, 216, 225, 243, 256, 288, 289, 324, 343, 361, > > > 392, 400, 432, 441, 484, 500, 512, 529, 576, 625, 648, 675, 676, 729, > > > 784, 800, 841, 864, 900, 961, 968, 972, 1000, 1024, 1089, 1125, 1152, > > > 1156, 1225, 1296, 1323, 1331, 1352, 1369, 1372, 1444, 1521, 1568, 1600, > > > 1681, 1728, 1764, 1800, 1849, 1936, 1944, 2000, 2025, 2048, 2116, 2187, > > > 2197, 2209, 2304, 2312, 2401, 2500} > > I really hope and expect that SAGE-programming will never look like the above! > > The more SAGE-an way: > > def is_powerful(n): > r""" > A Powerful Number: > A positive integer $n$ is powerful if for every prime $p$ dividing > $n$, $p^2$ also divides $n$. > > EXAMPLE > """ > for p in prime_divisors(n): > if n % p^2 > 0: > return False > return True > > def alist(n): > return [i for i in range(1,n+1) if is_powerful(i)] > > print alist(2500) > [1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 72, 81, 100, 108, 121, 125, 128, > 144, > 169, 196, 200, 216, 225, 243, 256, 288, 289, 324, 343, 361, 392, 400, 432, > 441, 484, 500, 512, 529, 576, 625, 648, 675, 676, 729, 784, 800, 841, 864, > 900, 961, 968, 972, 1000, 1024, 1089, 1125, 1152, 1156, 1225, 1296, 1323, > 1331, 1352, 1369, 1372, 1444, 1521, 1568, 1600, 1681, 1728, 1764, 1800, 1849, > 1936, 1944, 2000, 2025, 2048, 2116, 2187, 2197, 2209, 2304, 2312, 2401, 2500] > > In the OEIS there are a lot of Mathematics programs like this. We can do > better and will do better! > > Jaap > > > >
Hi everybody I'm a great fan of SAGE but please don't understimate the alternatives. Mathematica is a very powerful language even if its features can be misunderstood. For example I'd write a program for calculating Powerful numbers in the following way IsPowerful[n_] := Not[MemberQ[FactorInteger[n], 1, {2}]] This simply checks that "1" isn't an exponent of the factorization, i.e. each prime divisor has an exponent of at least 2. Then I'd make this function "Listable", i.e. that can work on entire lists SetAttributes[IsPowerful, Listable] Finally I'd apply the function IsPowerful to the entire Range from 2 to n and take the position where the function IsPowerful returns True. A little refinement is to put IsPowerful[1] = False; to make the final code simpler Position[IsPowerful[Range[2500]], True] {{4}, {8}, {9}, {16}, {25}, {27}, {32}, {36}, {49}, {64}, [cut] I don't think this four lines are more difficult to read than the corrispondent SAGE code and please note that there are no "if" or "for" statements (SAGE code has 2 if and 2 for). I'd expect Mathematica code to be faster than SAGE code, and however faster than the previous proposed Mathematica code. SAGE is fantastic but we can't "rest on our laurels" Regards Tiziano --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---