Thread: Help!! Problem 3 of Project Euler

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    Help!! Problem 3 of Project Euler

    Hi,
    I started Project Euler sometimes ago and I have managed to finish problem 1 and 2 but now I am stuck on problem 3.As it says, I have implemented the code for 13195 but it doesn't work for 600851475143.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
        
         int i;
         int j=0;
         int k=2;
         int n=0;
         int number=13196;
         int factor[100];
         int prime[100];
         int isNotPrime;
         int largestPrime;
         
        for(i=1;i<number;i++){
            
            if((number-1)%i==0){
                
                //Factor is stored in the factor array
                factor[j]=i;
                
                
                
                for(k=2;k<(factor[j]);k++){
                    
                    //if the remainder of jth factor and kth number is 0
                    if(factor[j]%k==0){
                        
                        //isNotPrime stores jth factor
                        isNotPrime=factor[j];
                        
                    }
                }
                
                //If jth factor is not 1 and jth factor is not stored as isNotPrime
                if(factor[j]!=1 && isNotPrime!=factor[j]){
                    
                    //prime stores jth factor at nth location
                    prime[n]=factor[j];
                    
                    
                    //if nth prime is bigger than prime at first postion
                    if(prime[n]>prime[0]){
                        
                        //maxPrime stores nth prime
                        largestPrime=prime[n];
                    
                        
                    }else{
                        
                        //prime at first position is largest prime
                        largestPrime=prime[0];
                        
                    }
                    n++;
                }
                
                j++;
                
                
            }
            
            
        } 
            //Print results
             printf("The largest prime factor of 13195 is ");
            printf("%d\n",largestPrime);
        
    return EXIT_SUCCESS;    
    }
    Last edited by i++; 08-15-2013 at 12:56 PM.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    What's the maximum value a signed int can hold on your system?

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    How do I check that??

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Rather than checking, I would just #include <stdint.h> and start using uint64_t. I had to use 64 bit integers to solve this when I did it. C99 added the standard int library.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    This doesn't work!!!

    It doesn't display anything

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    
    int main(void){
        
          uint64_t  i;
          uint64_t  j=0;
          uint64_t  k=2;
          uint64_t  n=0;
          uint64_t  number=600851475144;
          uint64_t  factor[100];
          uint64_t  prime[100];
          uint64_t  isNotPrime;
          uint64_t  largestPrime;
         
        for(i=1;i<number;i++){
            
            if((number-1)%i==0){
                
                //Factor is stored in the factor array
                factor[j]=i;
                
                
                
                for(k=2;k<(factor[j]);k++){
                    
                    //if the remainder of jth factor and kth number is 0
                    if(factor[j]%k==0){
                        
                        //isNotPrime stores jth factor
                        isNotPrime=factor[j];
                        
                    }
                }
                
                //If jth factor is not 1 and jth factor is not stored as isNotPrime
                if(factor[j]!=1 && isNotPrime!=factor[j]){
                    
                    //prime stores jth factor at nth location
                    prime[n]=factor[j];
                    
                    
                    //if nth prime is bigger than prime at first postion
                    if(prime[n]>prime[0]){
                        
                        //maxPrime stores nth prime
                        largestPrime=prime[n];
                    
                        
                    }else{
                        
                        //prime at first position is largest prime
                        largestPrime=prime[0];
                        
                    }
                    n++;
                }
                
                j++;
                
                
            }
            
            
        } 
            //Print results
             printf("The largest prime factor of 600851475143 is ");
            printf("%PRIu64 \n",largestPrime);
        
    return EXIT_SUCCESS;    
    }
    Last edited by i++; 08-15-2013 at 12:57 PM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Are you sure you are patient enough?

    Try add
    Code:
    printf("Factor[%llu]=%llu\n",j,i);
    on line 24 to not wait in the dark
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    Quote Originally Posted by vart View Post
    Are you sure you are patient enough?

    Try add
    Code:
    printf("Factor[%llu]=%llu\n",j,i);
    on line 24 to not wait in the dark
    It still doesn't work.I commented out some of the code and it seems it doesn't go beyond factor[14] for some reason.It doesn't even print out the number of factor it has like it should in the code below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    
    int main(void){
        
          uint64_t  i;
          uint64_t  j=0;
          uint64_t  k=2;
          uint64_t  n=0;
          uint64_t  number=600851475144;
          uint64_t  factor[1000];
          uint64_t  prime[1000];
          uint64_t  isNotPrime;
          uint64_t  largestPrime;
         
            for(i=1;i<number;i++){
            
                if((number-1)%i==0){
                
                //Factor is stored in the factor array
                factor[j]=i;
                
                printf("Factor[%llu]=%llu\n",j,i);
                
                j++;
                
                
                }
            
            
            } 
            
            printf("The factors of 600851475143 are %llu",j);
        
    return EXIT_SUCCESS;    
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I guess what people are neglecting to tell you is if you pick a really slow method it can take literally 100 years. If you want a speedy solution you need to think about fast ways to factor big numbers. The GIMPs don't use naive division to find primes and neither should you. Try using a sieve as part of your solution.

    Also, mathematically speaking, it is impossible for a prime factor of a composite number to be greater than the square root of the number.
    Last edited by whiteflags; 08-15-2013 at 05:59 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by whiteflags View Post
    Also, mathematically speaking, it is impossible for a prime factor of a composite number to be greater than the square root of the number.
    Not quite accurate. Factors come in pairs. The prime factors of 34 are 2 and 17. The square root of 34 is 5.831, and 17 is bigger than 5.831. However, I think you're implying that there is no need to check for prime factors beyond the square root of a number, since at least one half of any factor pair will be less than the square root. Once you go beyond the square root, you will start seeing the "inverse" pairs, i.e. you will get "2 and 17", and once you pass 5.831, you will get "17 and 2".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Euler Problem 2 Solution
    By i++ in forum C Programming
    Replies: 1
    Last Post: 08-10-2013, 09:24 AM
  2. Project Euler problem
    By nimitzhunter in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2012, 09:00 PM
  3. Project Euler Problem 8
    By deadrabbit in forum C Programming
    Replies: 2
    Last Post: 10-06-2011, 10:56 PM
  4. Project Euler Problem 14 Segfault
    By vincent01910 in forum C Programming
    Replies: 5
    Last Post: 11-04-2009, 05:56 AM
  5. Project Euler problem 3
    By SELFMADE in forum C Programming
    Replies: 7
    Last Post: 09-16-2009, 03:33 AM

Tags for this Thread