954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Project Euler problem

Doing Project Euler problem 3.
Don't know why code not working.

#include<iostream>
using namespace std;


//largest prime factor 317584931803.

bool Primechecker(int);


int main()
{
	int greatestprime=1;
	int limit = 317584931803;
	int runningcount=1;
	
	while(runningcount<limit/2)
	{
		if (Primechecker(runningcount)==true && limit%runningcount == 0)
			{
			greatestprime = runningcount;
			}
		runningcount++;
	}
	

	cout << greatestprime;
	cin.get();
	return 0;
}

bool Primechecker(int num)
{
	for (int i=1; i< num/2; i++)
	{
		if (num % i == 0 && i != 1)
			return false;
	}
	return true;
}
SurviBee
Newbie Poster
16 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Your limit is an int, which is too small to hold the number you have put in it. (Your compiler should have complained about this.) Try using a long long or somesuch.

Start runningcount at 2, not one.

Your primeChecker() function is fine, but it will be slow.

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You