Greatest prime dividing the n-th cubefree number: Difference between revisions
Content added Content deleted
(Added Python) |
(Added Dart) |
||
Line 24: | Line 24: | ||
;Reference |
;Reference |
||
* [https://oeis.org/A370833 OEIS sequence: A370833: a(n) is the greatest prime dividing the n-th cubefree number, for n >= 2; a(1)=1.] |
* [https://oeis.org/A370833 OEIS sequence: A370833: a(n) is the greatest prime dividing the n-th cubefree number, for n >= 2; a(1)=1.] |
||
=={{header|Dart}}== |
|||
{{trans|Wren}} |
|||
<syntaxhighlight lang="dart">import 'dart:math'; |
|||
void main() { |
|||
List<int> res = [1]; |
|||
int count = 1; |
|||
int i = 2; |
|||
int lim1 = 100; |
|||
int lim2 = 1000; |
|||
double max = 1e7; |
|||
var t0 = DateTime.now(); |
|||
while (count < max) { |
|||
bool cubeFree = false; |
|||
List<int> factors = primeFactors(i); |
|||
if (factors.length < 3) { |
|||
cubeFree = true; |
|||
} else { |
|||
cubeFree = true; |
|||
for (int i = 2; i < factors.length; i++) { |
|||
if (factors[i - 2] == factors[i - 1] && factors[i - 1] == factors[i]) { |
|||
cubeFree = false; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
if (cubeFree) { |
|||
if (count < lim1) res.add(factors.last); |
|||
count += 1; |
|||
if (count == lim1) { |
|||
print("First $lim1 terms of a[n]:"); |
|||
print(res.take(lim1).join(', ')); |
|||
print(""); |
|||
} else if (count == lim2) { |
|||
print("The $count term of a[n] is ${factors.last}"); |
|||
lim2 *= 10; |
|||
} |
|||
} |
|||
i += 1; |
|||
} |
|||
print("${DateTime.now().difference(t0).inSeconds} sec."); |
|||
} |
|||
List<int> primeFactors(int n) { |
|||
List<int> factors = []; |
|||
while (n % 2 == 0) { |
|||
factors.add(2); |
|||
n ~/= 2; |
|||
} |
|||
for (int i = 3; i <= sqrt(n); i += 2) { |
|||
while (n % i == 0) { |
|||
factors.add(i); |
|||
n ~/= i; |
|||
} |
|||
} |
|||
if (n > 2) { |
|||
factors.add(n); |
|||
} |
|||
return factors; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 100 terms of a[n]: |
|||
1, 2, 3, 2, 5, 3, 7, 3, 5, 11, 3, 13, 7, 5, 17, 3, 19, 5, 7, 11, 23, 5, 13, 7, 29, 5, 31, 11, 17, 7, 3, 37, 19, 13, 41, 7, 43, 11, 5, 23, 47, 7, 5, 17, 13, 53, 11, 19, 29, 59, 5, 61, 31, 7, 13, 11, 67, 17, 23, 7, 71, 73, 37, 5, 19, 11, 13, 79, 41, 83, 7, 17, 43, 29, 89, 5, 13, 23, 31, 47, 19, 97, 7, 11, 5, 101, 17, 103, 7, 53, 107, 109, 11, 37, 113, 19, 23, 29, 13, 59 |
|||
The 1000 term of a[n] is 109 |
|||
The 10000 term of a[n] is 101 |
|||
The 100000 term of a[n] is 1693 |
|||
The 1000000 term of a[n] is 1202057 |
|||
The 10000000 term of a[n] is 1202057</pre> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |