Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i just trying to count the prime divisors of a number....but getting error in counting help please.......

[edit]Code
C++
# 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;
}
block added - OriginalGriff[/edit]
Posted
Updated 28-Apr-16 3:22am
v3
Comments
Patrice T 28-Apr-16 6:38am    
What error ? Where ?
Patrice T 28-Apr-16 9:25am    
How many prime divisors do you expect for 36 ?
Philippe Mori 28-Apr-16 12:26pm    
Fix typos in your code.
Philippe Mori 28-Apr-16 12:34pm    
What are you trying to do with multiplication and division by 10?
Patrice T 28-Apr-16 14:01pm    
Manual conversion from string to int.

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:
C++
# include <stdio.h>
# include <math.h>
You shouldn;t have a space between '#' and 'include'
C++
#include

Doesn't load anything, so it will cause problems. Get rid of it.
C#
long long i=0,a=0;

What is a "long long"? You only need the type name once.
C#
for(int i=2; i<n;>    {
What happened to the increment? and the close bracket?
Try:
C#
for(int i=2; i<n; i++)    {


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
C#
bool myFunction()
{
   for (...) {
      statement;
   }
}
doesn't help you.
Stick to K&R:
C++
bool myFunction()
{
   for (...) 
   {
      statement;
   }
}
Or Whitesmiths:
C++
bool myFunction()
   {
   for (...) 
      {
      statement;
      }
   }
Don't try to use 1TB until you are well and truly familiar with the language itself.
 
Share this answer
 
Comments
OriginalGriff 28-Apr-16 7:20am    
Ah! Right... :O
Richard MacCutchan 28-Apr-16 7:37am    
Another disappointment in my life ... OG is fallible (almost) :(
OriginalGriff 28-Apr-16 7:57am    
Oh trust me, I make mistakes! But I do try never to make the same one twice...
And I really don't trust people who won't admit to theirs ... doctors and policemen for example :laugh:
[no name] 28-Apr-16 12:17pm    
Actually, this IS K&R style (open brace not on separate line):

bool myFunction() {
    for (...) {
        statement;
    }
}
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.
 
Share this answer
 
v4
try

C++
 #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;
}
 
Share this answer
 
v2
Comments
Philippe Mori 28-Apr-16 12:38pm    
Much better than original code but still many issues.... Like global variables, inconsistent types and unused variables.
CPallini 28-Apr-16 13:54pm    
You are right. I just did a bit of clean up.
Philippe Mori 28-Apr-16 12:39pm    
By the way, that code would be very slow for large numbers...
CPallini 28-Apr-16 13:55pm    
Yes, I know that. My code is just a quick fix of the OP one. Not meant to be a reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900