Cyclotomic polynomial: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(→{{header|Sidef}}: replaced with built-in solution) |
||
Line 4,371: | Line 4,371: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
Built-in: |
|||
Solution based on polynomial interpolation (slow). |
|||
<syntaxhighlight lang="ruby"> |
<syntaxhighlight lang="ruby">say "First 30 cyclotomic polynomials:" |
||
Poly.string_config(Hash(fold_sign => true, prefix => "", suffix => "")) |
|||
func poly_interpolation(v) { |
|||
v.len.of {|n| v.len.of {|k| n**k } }.msolve(v) |
|||
} |
|||
say "First 30 cyclotomic polynomials:" |
|||
for k in (1..30) { |
for k in (1..30) { |
||
say ("Φ(#{k}) = ", cyclotomic(k)) |
|||
var Φ = poly_interpolation(a) |
|||
say ("Φ(#{k}) = ", Poly.new(Φ...)) |
|||
} |
} |
||
say "\nSmallest cyclotomic polynomial with n or -n as a coefficient:" |
say "\nSmallest cyclotomic polynomial with n or -n as a coefficient:" |
||
for n in (1..10) { # |
for n in (1..10) { # slow |
||
var k = (1..Inf -> first {|k| |
var k = (1..Inf -> first {|k| |
||
cyclotomic(k).coeffs.any { .tail.abs == n } |
|||
}) |
}) |
||
say "Φ(#{k}) has coefficient with magnitude #{n}" |
say "Φ(#{k}) has coefficient with magnitude #{n}" |
||
}</syntaxhighlight> |
|||
Slightly faster solution, using the '''Math::Polynomial::Cyclotomic''' Perl module. |
|||
<syntaxhighlight lang="ruby">var Poly = require('Math::Polynomial') |
|||
require('Math::Polynomial::Cyclotomic') |
|||
Poly.string_config(Hash(fold_sign => true, prefix => "", suffix => "")) |
|||
say "First 30 cyclotomic polynomials:" |
|||
for k in (1..30) { |
|||
say ("Φ(#{k}) = ", Poly.new.cyclotomic(k)) |
|||
} |
|||
say "\nSmallest cyclotomic polynomial with n or -n as a coefficient:" |
|||
for n in (1..10) { |
|||
var p = Poly.new |
|||
var k = (1..Inf -> first {|k| |
|||
[p.cyclotomic(k).coeff].first { .abs == n } |
|||
}) |
|||
say "Φ(#{k}) has coefficient with magnitude = #{n}" |
|||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Line 4,458: | Line 4,429: | ||
^C |
^C |
||
</pre> |
</pre> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C++}} |
{{trans|C++}} |