Thread: divide array and value & array value in if() statement

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by _jamie View Post
    this is what I've came up so far, next step for me is work on adding the code for the largest prime factor instead of all 4 of them being printed out. Thanks to all on the forum for the continuous help. FYI the numbers in the new code i am posting is a 12 digit long number which is part of the actual question instead of a 5 digit int on my first post. Problem 3 - Project Euler

    Code:
    /* What is the largest prime factor of the number 600851475143 ? */
    // 6857
    
    
    #include <stdio.h>
    
    
    int main (void)
    {
        long num = 600851475143;
        long a = 2;
    
    
        // while (a < num)
        while (num != 1)
        {
            if (num % a == 0)
            {
                while (num % a == 0)
                {
                    num = num / a;
                    printf("%ld, ", a);
                }
    
    
            }
            a++;
        }
        
        printf("\n");
        
        return 0;
    }
    Well, you've already done it. Remove line 22 (the printf in the loop) and change line 30 to simply print the value of a

    Edit:
    Code:
    $ factor 600851475143
    600851475143: 71 839 1471 6857
    You're eliminating factors in ascending order so the value of 'a' when the while loop finishes is the largest factor. If you want to be paranoid I guess you could write an isPrime function and check that 6857 is truly prime (but it must be because you've already eliminate any prime or composite factors less than 6857, so 6857 must be prime) (and the command line factor program prints prime factors only, so if you're getting 6857 then...)
    Last edited by Hodor; 01-28-2020 at 11:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statement for array ?
    By Junior Aj Twist in forum C Programming
    Replies: 1
    Last Post: 11-17-2013, 01:39 PM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM
  5. array of pfuncs in one statement
    By genghis in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2003, 02:56 PM

Tags for this Thread