计算一个数的素数除数.. [英] Counting prime divisors of a number..

查看:79
本文介绍了计算一个数的素数除数..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想算一个数字的主要除数......但是在计算帮助时得到错误请.......



代码

i just trying to count the prime divisors of a number....but getting error in counting help please.......

[edit]Code

# include <stdio.h>
# include <math.h>
#include
#include <iostream>
using namespace std;

long long i=0,a=0;
//long long count=0;
bool prime(int n)
{
	for(int i=2; i<n;>    {
		if(n%i==0)
			return 0;
	}
	return 1;
}

int divisors(long long n)
{
	int check=0;
	for(i=n/10; i    {
		bool p=prime(i);     //checking the value of i is prime or not
		if(p==1)
		{
			if(n%i==0)      //counting prime divisors for current number,here i am getting error
			{
				check++;
				a=i;
			}
		}
	}
	return check;     //returning the number of prime divisors of current number
}

int main()
{
	string s;
	while(cin>>s)        //taking a at most 14 digit number
	{
		int len=s.size(),count=0;
		long long number=0;
		for(int i=0; i<len;>        {
			int digit=s[i]-'0';
			number*=10;
			number+=digit;          //converting into number
			count+=divisors(number);//counting the prime divisors of the current number
		}

		if(count>1)
			cout<<a<<endl;
		else
			cout<<"-1\n";
	}
	return 0;
}

已添加块 - OriginalGriff [/ edit ]

block added - OriginalGriff[/edit]

推荐答案

在开始运行代码之前很久就会出现错误 - 这会导致错误;编译 - 这可能就是收到错误的原因。


首先查看编译器输出 - 在大多数IDE中,如果双击错误信息,它将带您到相应的行。

显而易见的开始是:

You're getting errors long before you start running the code - that won;t compile - which is probably why you are "getting error".

Start by looking at the compiler output - in most IDE's if you double click an error message it will take you to the appropriate line.
The obvious ones to start with are:
# include <stdio.h>
# include <math.h>

你不应该在'#'和'include'之间留一个空格

You shouldn;t have a space between '#' and 'include'

#include



不加载任何内容,所以它会引起问题。摆脱它。


Doesn't load anything, so it will cause problems. Get rid of it.

long long i=0,a=0;



什么是 long long ?您只需要一次类型名称。


What is a "long long"? You only need the type name once.

for(int i=2; i<n;>    {

增量发生了什么?和关闭括号?

尝试:

What happened to the increment? and the close bracket?
Try:

for(int i=2; i<n; i++)    {





会有其他人,但我不是编译器 - 所以请使用你的并阅读错误信息!



你应该考虑的其他事情:

选择一种包围式并坚持下去:将1TB与K& R混合在一个简单的应用程序中是愚蠢的,因为它让你很难阅读,别介意其他人。

所以



There will be others, but I'm not a compiler - so use yours and read the error messages!

Other things you should think about:
Pick a bracketing style and stick to it: mixing 1TB with K&R in a trivial app is just silly because it makes it hard for you to read, never mind anyone else.
So

bool myFunction()
{
   for (...) {
      statement;
   }
}

对你没有帮助。

坚持K& R:

doesn't help you.
Stick to K&R:

bool myFunction()
{
   for (...) 
   {
      statement;
   }
}

或Whitesmiths:

Or Whitesmiths:

bool myFunction()
   {
   for (...) 
      {
      statement;
      }
   }

在你熟悉语言本身之前不要尝试使用1TB。

Don't try to use 1TB until you are well and truly familiar with the language itself.


你应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



程序编译完成后,您绝对需要在调试器上运行它以了解它在做什么。 br />
逐行运行程序并检查变量以查看每行的作用。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Once your program compile, you will absolutely need to run it on debugger to understand what it is doing.
Run the program line by line and inspect variables to see what each line is doing.


试试



try

 #include <stdio.h>
 #include <math.h>
 #include <iostream>
using namespace std;


bool prime(long long n)
{
  for(int i=2; i<n; ++i)
  {
    if (n % i == 0 ) return false;
  }
  return true;
}

int divisors(long long n)
{
  int check =  0;

  for (i=2; i<n; ++i)
  {
    if ( (n% i) == 0)
    {
      cout << "divisor " << i << " is ";
      if (prime(i))
      {
        cout << "prime ";
        check++;
      }
      else
        cout << "not prime";
      cout << endl;
    }
  }
  return check;
}

int main()
{
    long long number;

    cout << "please enter a number: " << endl;
    cin >> number;

    cout << "there are " << divisors(number) << " prime divisors of " << number << endl;
    return 0;
}


这篇关于计算一个数的素数除数..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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