如果有任何简单的方法来计算素数因子,则显示素数因子(半素因子到第15个素因子) [英] displaying prime factors (semi prime factor to 15th prime factor) if any simple way to caluclate to find prime factors

查看:157
本文介绍了如果有任何简单的方法来计算素数因子,则显示素数因子(半素因子到第15个素因子)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生,

i有一个文本框,一个按钮和一个gridview控件,当我在文本框中输入1000的数字并点击按钮我需要显示所有的素因子数字gridview colums(即half,tri,quad,quint,sext和upto 15th prime factor number)



如果有人知道这个请帮我解决它.... ..........................



i不知道如何计算素因子到15thprime factor

解决方案

一个简单的谷歌搜索会找到很多解释如何找到素数的文章。 CodeProject文章部分中还有一个数字[ ^ ]。


假设有一个上限在最大的素数(你说的第15个素数将是47:见这里 [ ^ ],您可以轻松构建一个包含所有允许的数组然后从最小的素数(2)开始到最大的(如果我的假设是正确的话47)你迭代所述数组并尝试将要分解的数字除以当前素数的模数为零(否)剩余的)。



0.如果当前的主要转到3不能没有剩余的数字。

1.如果t他可以将数字除以当前素数而不用余数,将要测试的数字设置为除以素数的数字,并将当前素数的指数设置为1。

2.只要没有剩余部分的部门会做1.

3.从数组中选择下一个素数并继续0.



4.迭代必须当分解的数字变为1时停止,因为那样分解就完成了。如果我的假设是正确的,你也可以在你用完素数时停止分解。减少的数字将是设定限制下的分解的剩余部分。



有趣的注意事项为什么一个是终止条件:



所有数字都可以用一组元组表示{(n 0 ,m 0 ),...,(n i ,m i )}其中元组的第一部分m i 是素数,第二部分m i 是该素数的指数。因此,分解数可以表示为:b
0 m 0 *。 .. * n i m i



那里的所有素数是。值得庆幸的是,我们不必包括所有的素数(即使我们想要它们的数量是无限的,我们也不能这样做),因为任何提高到0的幂的数字都是1,因此不会以任何方式改变产品。



由于数字1不能被分解,所有可能素数的指数为0,每个素数 0 。所有无限数量的素数的乘积提升到0的次数再为1.





我希望这不是''令人困惑。



问候,



- Manfred

伪代码中可能的解决方案:

 1。得到要计算的条目数(例如1000)
2.建立Eratosthenes的筛子,直到该数字的平方根
3.计算每个数的素数因子从2到给定通过在预先计算的Eratosthenes筛选中查找因子来计算



Eratosthenes筛选:参见维基百科 [ ^ ] 。



用伪代码计算一个数字 N 的因子:

< pre lang =text> 1。结束如果N小于2
2.在Eratosthenes筛选中找到一个条目,如果N被该条目划分为
3.则不给予休息。如果不是这样的条目,则N是因子
4.否则,条目是因子
5.将N除以因子并将新N带到第1步。



干杯
Andi


Hello sir,
i have one textbox, one button, and one gridview control when i was enter the number in textbox for 1000 and click on the button i need to display all the prime factor numbers in gridview colums (i.e semi,tri,quad,quint,sext and upto 15th prime factor numbers)

if anyone know this please help me to solve it..............................

i dont know how to caluclate prime factors up to 15thprime factor

解决方案

A simple Google search will find you lots of articles that explain how to find prime numbers. And there are also a number in the CodeProject articles section[^].


Assuming that there is an upper bound on the largest prime (you said something about the 15th prime which would be 47: see here[^], you can easily construct an array containing all the allowed primes. Then starting from the smallest prime (2) towards the largest (47 if my assumption was correct) you iterate over said array and try if the number to be factorized can be divided by the current prime number with a modulo of zero (no remainder).

0. If the number cannot be divided without remainder by the current prime goto 3.
1. If the number can be divided by the current prime without remainder, set the number to be tested to the number divided by the prime and set the exponent of the current prime to one.
2. As long as the division stays without remainder do 1.
3. Choose the next prime from the array and continue with 0.

4. The iteration must stop when the number to factorized becomes one, because then the factorization is done. In case my assumption was correct you can also stop the factorization if you run out of primes. The reduced number will then be the remainder of the factorization under the set limitations.

Interesting side note why one is the termination condition:

All numbers can be expressed by a set of tuples {(n0,m0),..., (ni, mi) } where the first part mi of the tuple is the prime number and the second part mi is the exponent for that prime. The factorized number can thus be expressed as

m0m0 * ... * nimi

for all the prime numbers there are. Thankfully we don''t have to include all the prime numbers there are (and we couldn''t do so either even if we wanted as their number is infinite), because any number raised to the power of zero is one an and thus will not change the product in any way.

Since the number 1 cannot be factorized, the exponents for all possible primes are 0 giving a one for each prime number0 . The product of all infinite number of primes raised to the power of zero is 1 then again 1.


I hope this wasn''t to confusing.

Regards,

— Manfred


A possible solution in pseudo code:

1. get the number of entries to calculate (e.g. 1000)
2. build the "Sieve of Eratosthenes" up to the square root of that number
3. calculate the prime factors of each number from 2 to that given number
   by looking up factors in the pre-calculated Sieve of Eratosthenes


Sieve of Eratosthenes: see Wikipedia[^].

Calculating the factors of one number N in pseudo code:

1. end if N is less than 2
2. find in the Sieve of Eratosthenes an entry that gives no rest if N is devided by that entry
3. if not such entry is found N is the factor
4. else, the entry is the factor
5. divide N by the factor and take that new N to step 1.


Cheers
Andi


这篇关于如果有任何简单的方法来计算素数因子,则显示素数因子(半素因子到第15个素因子)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆