Problem 003

A place to air possible concerns or difficulties in understanding ProjectEuler problems. This forum is not meant to publish solutions. This forum is NOT meant to discuss solution methods or giving hints how a problem can be solved.
Forum rules
As your posts will be visible to the general public you are requested to be thoughtful in not posting anything that might explicitly give away how to solve a particular problem.

This forum is NOT meant to discuss solution methods for a problem.

In particular don't post any code fragments or results.

Don't start begging others to give partial answers to problems

Don't ask for hints how to solve a problem

Don't start a new topic for a problem if there already exists one


See also the topics:
Don't post any spoilers
Comments, questions and clarifications about PE problems.
User avatar
hk
Administrator
Posts: 12832
Joined: Sun Mar 26, 2006 10:34 am
Location: Haren, Netherlands

Re: Problem 003

Post by hk »

Ocifer wrote:I've looked at the Sieve of Eratosthenes, and similar methods, but I don't see the advantage; one would still have to store the primes up the number being factored...
Actually only up to the square root of the number being factored.
Please read the overview/pdf provided by the problem.
Image
War ruins the life and health of untold numbers of innocent children.
Ocifer
Posts: 4
Joined: Thu Nov 25, 2010 7:09 pm

Re: Problem 003

Post by Ocifer »

hk wrote:
Ocifer wrote:I've looked at the Sieve of Eratosthenes, and similar methods, but I don't see the advantage; one would still have to store the primes up the number being factored...
Actually only up to the square root of the number being factored.
Please read the overview/pdf provided by the problem.
Where do I find this pdf? Can you please explain why it's efficient to store all the primes up the square root? I understand it's a lesser number and therefore the algorithm will terminate faster, but as the numbers get big the square root will also be rather large. If I do adopt this method, should I allocate an array? If I opt for dynamic memory allocation, there's no guarantee my program will work if there are too many primes for it to store.
TripleM
Posts: 384
Joined: Fri Sep 12, 2008 3:31 am

Re: Problem 003

Post by TripleM »

You'll see a PDF icon to the right of the green checkmark (for some solved problems) on the problem listing page: http://projecteuler.net/index.php?section=problems
Ocifer
Posts: 4
Joined: Thu Nov 25, 2010 7:09 pm

Re: Problem 003

Post by Ocifer »

Thanks, TripleM. The PDF helped. A hint to anyone that is experiencing the same confusion I had, you are not actually meant to store the prime numbers generated by the Sieve method; rather, you are meant to use a fact derived from the Sieve of Eratosthenes to improve the bounds of your factoring algorithm.
anant718
Posts: 3
Joined: Thu Feb 03, 2011 6:36 am

Re: Problem 003

Post by anant718 »

My code can solve for only small numbers,as the one given as sample in the question.But the console window dosent display anything when i apply the code on the number whose prime factor is required.What do i do?is there some other way to handle large numbers like these?I even used long type.Please help.
User avatar
stijn263
Posts: 1505
Joined: Sat Sep 15, 2007 11:57 pm
Location: Netherlands

Re: Problem 003

Post by stijn263 »

It would help if you told us your programming language. In C++, one of the following should work:
long long A = 600851475143ll;
or
__int64 A = 600851475143ll;
anant718
Posts: 3
Joined: Thu Feb 03, 2011 6:36 am

Re: Problem 003

Post by anant718 »

I've recently started working in c# and tried writing the code in it only.
User avatar
stijn263
Posts: 1505
Joined: Sat Sep 15, 2007 11:57 pm
Location: Netherlands

Re: Problem 003

Post by stijn263 »

perhaps Int64 or int64 works? You can check the limits of int64 with something like this:

Int64 p = 10;
Int64 x = 1;

x = x*p;
x = x*p;
x = x*p;
x = x*p;
x = x*p;

etc (I'd suggest writing a loop, but I wouldn't know how to do that in C#)
anant718
Posts: 3
Joined: Thu Feb 03, 2011 6:36 am

Re: Problem 003

Post by anant718 »

Thanks a ton...I figured that out myself today....
wygk123
Posts: 3
Joined: Wed Feb 09, 2011 2:10 pm

Re: Problem 003

Post by wygk123 »

}
Last edited by wygk123 on Wed Feb 09, 2011 3:14 pm, edited 1 time in total.
User avatar
hk
Administrator
Posts: 12832
Joined: Sun Mar 26, 2006 10:34 am
Location: Haren, Netherlands

Re: Problem 003

Post by hk »

Posting solutions is against forum rules.
Please remove them.
I sent you a warning.
Image
War ruins the life and health of untold numbers of innocent children.
horance_89
Posts: 1
Joined: Thu Mar 03, 2011 6:26 pm

Re: Problem 003

Post by horance_89 »

I have done a c++ little program and i think i got the result!
But woldn´t work inputed..
look at my code a little(the part with getprimefactor isn´t made by me)


Code: Select all

snip


So...i guess it should work fine!
Your opinions?
Edited:
Oks..sry about code posted..but i still do not understand why wouldn´t get my answer!??
Last edited by horance_89 on Mon Mar 07, 2011 6:04 pm, edited 1 time in total.
User avatar
rayfil
Administrator
Posts: 1412
Joined: Sun Mar 26, 2006 5:30 am
Location: Quebec, Canada
Contact:

Re: Problem 003

Post by rayfil »

Sorry. Your code got snipped. Maybe you forgot to read the large red letters at the top of the page:
In particular don't post any code fragments or results.
When you assume something, you risk being wrong half the time.
zlaw777
Posts: 3
Joined: Sat Apr 02, 2011 5:15 am

Re: Problem 003

Post by zlaw777 »

My code is working in Java for the number 13195. However, it does not work for the large number simply because Java is saying the number is too large. I tried using long instead of int, this did not help. How do I use large numbers in Java?
User avatar
jaap
Posts: 588
Joined: Tue Mar 25, 2008 3:57 pm
Contact:

Re: Problem 003

Post by jaap »

zlaw777 wrote:My code is working in Java for the number 13195. However, it does not work for the large number simply because Java is saying the number is too large. I tried using long instead of int, this did not help. How do I use large numbers in Java?
Try something like:

Code: Select all

long n = 600851475143L;
Note the L at the end of the number to indicate that it is a long. Without that the literal number is assumed to denote an integer (which is then converted to long for the assignment to n). The literal number (without L) however is not a valid integer so it won't compile.
swartzism
Posts: 2
Joined: Tue Apr 05, 2011 3:17 pm

Re: Problem 003

Post by swartzism »

I believe I have a correct algorithm to solve this problem, but the number in this problem is making my computer go haywire. If I do a little test to see if gcc on cygwin (windows) can handle the 600851... number, it gives some strange results. Here is my test code: (does not have to do with solving this problem at all! Don't worry!)

Code: Select all

#include <stdio.h>
#include <math.h>

int main()
{
   long long n = 600851475143ll; // have also tried with LL
   long long o = sqrt(n);

   printf("%d\n", n);
   printf("%d\n", o);

   return 0;
}
The results I get are:
-443942297
775146
I believe the resulting number of printing out "n" is why my program is not working. Any ideas why this is what I'm getting?

Thanks in advance.
User avatar
jaap
Posts: 588
Joined: Tue Mar 25, 2008 3:57 pm
Contact:

Re: Problem 003

Post by jaap »

Try this:

Code: Select all

printf("%lld\n", n);
%d is only for ints, %ld is for longs, and %lld is the most supported format tag for long longs.
parliament718
Posts: 1
Joined: Thu May 05, 2011 3:13 am

Please take a look at this problem

Post by parliament718 »

Hi, Ive been trying to do problem #3 for some time now, I've built a program that finds the prime factors of many numbers successfully including 13975 which is included in the problem. However when i try to solve it for the number 600851475143 i get the output: the prime factors for 6.00851 e11 is 2... and thats it... just 2 which is clearly wrong. Can somebody tell me where I'm going wrong. I'm not sure where I could ask for help but I made sure not to put it in the section where there are no spoilers allowed.

Here's the code and thanks in advance.

Code: Select all

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


// This program will take a number and find its divisors, then store them in an doubleerger vector.

void findPrimeDivisors(double, double, vector<double> &);
bool isPrime (double);

void main()
{

  double number;
  double vectorindex = 0;
  vector <double> divisors;
  vector <double> primes;
  


  cout << "Please enter the number for which you would like to find the divisors: " << endl;
  cin >> number;
  cout << "The primefactors for " << number <<" are: " ;
  findPrimeDivisors (number, vectorindex, divisors);
  cout<< endl;
  
  cin >> number; //Delay program exit
}

void findPrimeDivisors(double num, double index,  vector<double> &integers)
{
  for(double counter = 1; counter <= num; counter ++)
  {
       
    if ( (int)num % (int)counter == 0) // Checking if 'counter' is a divisor of 'number'
      {
		 if ( isPrime(counter) )       // If it's a divisor, checking if it's prime.
         {
		 integers.push_back(counter);
         cout << integers[index] << " ";
         index ++;   
		 }
      }
	
  }   
}

 bool isPrime (double num)
{

	if (num <=1)
		return false;
	else if (num == 2)         
		return true;
	else if ((int)num % 2 == 0)
		return false;
	else
	{
		bool prime = true;
		double divisor = 3;
		double num_d = static_cast<double>(num);
		double upperLimit = static_cast<double>(sqrt(num_d) +1);
        
		while (divisor <= upperLimit)
		{
			if ((int)num % (int)divisor == 0)
				prime = false;
			divisor +=2;
		}
		return prime;
	}
}
User avatar
rayfil
Administrator
Posts: 1412
Joined: Sun Mar 26, 2006 5:30 am
Location: Quebec, Canada
Contact:

Re: Problem 003

Post by rayfil »

@parliament718

Spoilers are NOT allowed ANYWHERE on this open forum.

Since your post is related to a specific PE problem, your post has been transfered to the appropriate sub-forum and topic. FYI, problem numbers under #100 are padded with leading 0's to have a standard 3-digit number for ease of listing and searching.

You may even find an answer to your dilemma by reading this topic. If you do find the answer, you should edit your post to remove your code in part or its entirety. Welcome to this open forum.
When you assume something, you risk being wrong half the time.
User avatar
GenePeer
Posts: 112
Joined: Sat Apr 03, 2010 1:14 pm
Contact:

Re: Problem 003

Post by GenePeer »

1) 600851475143 isn't even, so how can 2 be a factor?
2) Why are you using double? 6.00851e11 is not equal to 600851475143
3) I don't know how fast your computer is, but I doubt it would finish this loop "for(c=1 ; c<600851475143; c++)" in less than a minute.
Image
Post Reply