找到n的独特素因子 [英] finding unique prime factors of n

查看:138
本文介绍了找到n的独特素因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到n的唯一素因子,所以如果n = 126,那么素数是2 3 3 7而唯一的素数是2 3 7



我无法取出额外的三个。

I am trying to find the unique prime factors of n, so if n = 126, then the primes are 2 3 3 7 whereas the unique ones are 2 3 7

I'm having trouble taking out that extra three.

int findPrime(unsigned long n)
{
  int testDivisor, i;
  testDivisor = 2;
  i = 0;
  int ii = 0;
  while (testDivisor < n + 1)
  {
    while ((n % testDivisor) == 0)
    {
      prime[ii] = testDivisor;
      if (DEBUG == 1) printf("prime[%d] = %d\n", i, prime[i]);
      i++;
      n = n / testDivisor;
    }
    testDivisor++;
  }

}

推荐答案

为了我遇到了麻烦取出额外的三个,你需要首先在阵列中保持主要因素,然后从中挑选出独特的因素进行打印。或者在找到时打印它,但要跟踪它,如果它已经被打印,那么你就忽略它。
In order to "I'm having trouble taking out that extra three", you need to first maintain the prime factors in an array or so and then pick the unique ones out of it to print. Or print it as and when found but keep a track of it such that if it has already been printed then you ignore it.


我写了一个程序来解决你的问题。我创建了另一个变量来阻止程序打印相同的因子。



I wrote a program which solves your problem. I created another variable which prevents the program from printing the same factors.

#include<stdio.h>

primefactors(unsigned long a)
{
	int prime_factor = 2, nodoublefactor = 0;
	
	while(a!=1)
	{
			while(a%prime_factor == 0)
			{
				a = a/prime_factor;
				
				if(nodoublefactor!=prime_factor)
				{
					printf("%d ",prime_factor);
					
					nodoublefactor = prime_factor;
				}
			}
			prime_factor++;
	}
}

main()
{
	unsigned long number_given_by_user; 
	
	printf("Enter a number to find it's unique prime factors : ");
	scanf("%lu",&number_given_by_user);
	
	primefactors(number_given_by_user);
}


这篇关于找到n的独特素因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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