如何使用 Perl 对整数进行因式分解?

发布于 2024-07-29 22:05:39 字数 459 浏览 4 评论 0原文

我想将整数拆分为它们的因子。 例如,如果记录总数为:

169 - ( 13 x 13 times) 
146 - ( 73 x 2 times) 
150 - ( 50 x 3 times)
175 - ( 25 x 7 times)
168 - ( 84 x 2 ) 
160 - ( 80 x 2 times) 

当超过 10k 时 - 我希望所有内容都为 1000 当它超过 100k 时 - 我希望一切都在 10k 上

,这样我想对数字进行因式分解。 如何实现这一目标? 有没有 Perl 模块可用于此类数字运算?

假设记录总数为 10k。 它应该只被分割1000x10次; 不是 100 或 10 秒。

我可以使用 sqrt 函数。 但这并不总是我所期待的。 如果我输入 146,我必须得到 (73, 2)。

I want split integers into their factors. For example, if the total number of records is:

169 - ( 13 x 13 times) 
146 - ( 73 x 2 times) 
150 - ( 50 x 3 times)
175 - ( 25 x 7 times)
168 - ( 84 x 2 ) 
160 - ( 80 x 2 times) 

When it's more than 10k - I want everything on 1000
When it's more than 100k - I want everything on 10k

In this way I want to factor the number. How to achieve this? Is there any Perl module available for these kinds of number operations?

Suppose total number of records is 10k. It should be split by 1000x10 times only; not by 100 or 10s.

I can use sqrt function. But it's not always what I am expecting. If I give the input 146, I have to get (73, 2).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清欢 2024-08-05 22:05:40

循环遍历可接受范围内的一些常见数字(例如 9 到 15),计算测试数字的余数,然后选择最小值。

sub compute_width {
    my ($total_records) = @_;
    my %remainders;
    for(my $width = 9; $width <= 15; $width += 1) {
      my $remainder = $total_records % $width;
      $remainders{$width} = $remainder;
    }
    my @widths = sort { 
      $remainders{$a} <=> $remainders{$b} || 
      $a <=> $b 
    } keys %remainders;
    return $widths[0];
}

Loop through some common numbers in an acceptable range (say, 9 to 15), compute the remainder modulo your test number, and choose the lowest.

sub compute_width {
    my ($total_records) = @_;
    my %remainders;
    for(my $width = 9; $width <= 15; $width += 1) {
      my $remainder = $total_records % $width;
      $remainders{$width} = $remainder;
    }
    my @widths = sort { 
      $remainders{$a} <=> $remainders{$b} || 
      $a <=> $b 
    } keys %remainders;
    return $widths[0];
}
燃情 2024-08-05 22:05:40

如果数字不是质数,则可以使用因式分解算法。

这里有一个这样的函数的例子: http://www.classhelper.org/articles/perl-by-example-factoring-numbers/factoring-numbers-with-perl.shtml

If the number is not a prime you can use a factoring algorithm.

There is an example of such a function here: http://www.classhelper.org/articles/perl-by-example-factoring-numbers/factoring-numbers-with-perl.shtml

原谅过去的我 2024-08-05 22:05:39

您可以在 Perl 中使用与其他语言相同的算法。 这些想法中没有任何 Perl 特殊的魔力。 这只是实现,对于像这个问题这样的问题,它可能看起来与任何语言的实现非常相似。

您想解决什么问题? 如果我们知道您想要做什么,也许我们可以为您指出正确的算法:

  • 为什么超过 10,000 的数字必须使用 1,000 因子? 大多数数字都没有 1,000 因数。
  • 您想要所有因子,还是只想要最大的因子及其伴生因子?
  • sqrt 函数没有按您的预期工作是什么意思? 如果您遵循通用算法,则只需迭代到平方根的下限即可测试因子。 大多数整数没有整数平方根。

You can use the same algorithms you find for other languages in Perl. There isn't any Perl special magic in the ideas. It's just the implementation, and for something like this problem, it's probably going to look very similar to the implementation in any language.

What problem are you trying to solve? Maybe we can point you at the right algorithm if we know what you are trying to do:

  • Why must numbers over 10,000 use the 1,000 factor? Most numbers won't have a 1,000 factor.
  • Do you want all the factors, or just the largest and its companion?
  • What do you mean that the sqrt function doesn't work as you expect? If you're following the common algorithm, you just need to iterate up to the floor of the square root to test for factors. Most integers don't have an integral square root.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文