Thread: last prime factor

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    last prime factor

    Hi im new here and i did a search to try to find anything related but couldnt find anything... im new to c, i know some web programing like html and php. I need to create a program that you can imput 2 numbers and it gives back the highest prime number by which you can divide the number... something like this:

    Number last prime factor

    100 5
    101 101
    102 17
    103 103
    104 13
    105 7


    i tried to do some things but i didnt even came close

  2. #2
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    Well you could take a simple brute force type look on it.

    Step 1: Calculate all primes less than or equal to the number given.
    Step 2: If the number given is a prime that's it.
    Step 3: Divide it by each prime calculated until the remainder is 0.
    Step 4: If nothing produced a remainder of 0 display an error or whatever.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Show us your code... I have had this assignment before, and I assume this is homework.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    Yes it is homework... i would apreciate any help... this script does almost anything...

    Code:
    #include<stdio.h>
    
    //declaracion de variables. 
    int m, n, I, num;
    
    
    void LeerDatos()
    {
        printf("Ingrese el Primer Numero: ");
        scanf("%d",&m);
        
        printf("Ingrese el Segundo Numero: ");
        scanf("%d",&n);
    }
    
    void ejecutar()
    { 
        for (num = m; num <= n; num++) //el contador num  va de m a n
        {  
           for(I = 1; I <= num; I++)//se utiliza la variable I como contador para dividir al "num"
           {
                  if (num%I == 0) //si el residuo de num/I es igual a 0,etonces "I" es  un divisor de "num".
                  { 
                        printf("\n%hd es divisor de %hd.",I,num);
                        if (I == num)
                        printf("%s","\n"); 
                  
                  } 
            }
         }   
    }
    
    
    main()
    {
    LeerDatos();
    ejecutar();
    getchar();
    getchar();
    
    }

    Wraithan Show us your code... I have had this assignment before, and I assume this is homework.
    Last edited by frango9000; 07-26-2006 at 06:03 PM.

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    I advise you to edit out that last line before you get a verbal punch to the balls (read the guidelines if you're curious why such requests are disapproved of, or lookup the word cheating in the dictionary).
    Look around http://www.google.com/search?q=prime+number+algorithm for a prime number algorithm.
    Last edited by valis; 07-26-2006 at 06:00 PM.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    sorry already edit, the thing is that i have to present it tomorrow and i have been working on it all week... besides we could ask for help

  7. #7
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    It's not whether you are allowed to ask for help or not, and having source to look at is very helpful. But I think a lot of people here would consider that cheating and most people on the board are pretty short with cheaters.
    You're sort of on the right track with your current code you're just missing a chunk.

    First calculate all primes less than or equal to the number you are given and store them in an array--the first hit for the link I posted above has a whole bunch of algorithms to do that.

    Then do something like this:
    Code:
    for (i = total_number_of_primes_calculated; i != 0; --i)
    {
        if ((list_of_primes[i] % the_number_given) == 0)
            printf("The greatest prime which can evenly divide %i is %i", the_number_given, list_of_primes[i]);
    }
    Don't worry about asking for help, just don't ask for an entire program pre-written to do your homework.

    edit:
    The above code assumes the array of primes is sorted least to greatest.
    Last edited by valis; 07-26-2006 at 07:12 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    getchar();
    getchar();
    If there are more characters than you think in the input buffer, that won't work. Use
    Code:
    int c;  /* must be an int */
    while((c = getchar()) != '\n' && c != EOF);
    getchar();
    See the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with prime factor for 12 digit number
    By nick2 in forum C Programming
    Replies: 15
    Last Post: 06-19-2009, 04:39 AM
  2. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  3. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM