Factorising problem

This is a discussion on Factorising problem within the C Programming forums, part of the General Programming Boards category; I am writing a simple prgram to show that factors of an integer input I'm not sure if i'm going ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Factorising problem

    I am writing a simple prgram to show that factors of an integer input

    I'm not sure if i'm going about writing this the right way, I get an output, but I need to somehow restrict the display to only show the integer results instead of all the results i have written the following:

    Code:
    #include <stdio.h>
    
    int main (void) {
    
    int input;
    int a;
    int k;
    
    printf("Enter number to be factorised:\n");
    scanf("%d", &input);
    
    printf("the factors of %d are\n", input);
    
    for (k=1; k<=input; k++) {
    
    a = input/k;
    
    printf("%d\n", a);
    
    }
    
        return 0;
    }
    But the output of this is not what i want, say if i enter 7, t gives me

    Code:
    the factors of 7 are
    7
    3
    2
    1
    1
    1
    1
    but what i want it to show is, say if i enter 1001

    Code:
    the factors of 1001 are
    1001
    143
    91
    77
    13
    11
    7
    1
    any suggestions?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    If you want to check that k is a factor of input, check that it divides into input with no remainder.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    use something like
    Code:
    if (input%k==0)
    do not use
    Code:
    a = input/k;
    Also, try thinking of ways to optimize your algorithm

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Hmm.... my C coding probably blows, but here is what went through my snoodle.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BUFF 30
    
    int main (void) {
    
        int input, value, upper, k;
        static int array[BUFF];
        int i = 0;
    
        printf("Enter number to be factorised:\n");
    
        /*doesn't handle alphanumberic chars at the end of number*/
        if(scanf("%d", &input) != 1)
    	exit(1);
    
        printf("the factors of %d are\n", input);
    
        for (k=1 ; k<=input; k++) {
    	value = input %k;
    	if (value == 0) {
    	    array[i] = k;
    	    i++;
    	}
        }
        
        upper = i-1;
    
        for(;((upper>=0) && upper<BUFF);upper--) {
    	printf("%d\n", array[upper]);
        }
       
        return 0;
    }
    $gcc -g -Wall factor.c -o factor
    $./factor
    Enter number to be factorised:
    7
    the factors of 7 are
    7
    1
    $./factor
    Enter number to be factorised:
    1001
    the factors of 1001 are
    1001
    143
    91
    77
    13
    11
    7
    1
    $./factor
    Enter number to be factorised:
    8
    the factors of 8 are
    8
    4
    2
    1
    $./factor
    Enter number to be factorised:
    b
    $./factor
    Enter number to be factorised:
    b100
    $./factor
    Enter number to be factorised:
    adadade3
    $


    Ohh..... my aching hips.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    How about this one:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	long factor, input;
    	puts("Enter a number:");
    	scanf("%ld", &input);
    	
    	puts("The factors are:");
    	printf("%ld\n", input);
    	
    	for (factor = input/2; factor > 0; --factor)
    		if (input%factor==0)
    			printf("%ld\n", factor);
    	
    	return 0;
    }
    The following is a single run of the program:
    Enter a number:
    1001
    The factors are:
    1001
    143
    91
    77
    13
    11
    7
    1
    Last edited by noodles; 08-06-2006 at 10:16 AM.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Hmmm..... I wasn't aware you could do that. You win. Pull my finger.

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You might also see if you can improve further upon that. Everything you find that input % factor == 0, you can get two factors. factor is the obvious one, but input / factor is also one.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    	long factor, endfact, input, factor2;
    	puts("Enter a number:");
    	scanf("%ld", &input);
    
    	puts("The factors are:");
    	printf("1\n%ld\n", input); // 1, input are always factors.
    
    	for(factor = 2, endfact = sqrt(input); factor <= endfact; ++factor)
    	{
    		if(input % factor == 0)
    		{
    			factor2 = input / factor;
    			printf("%ld\n", factor);
    			if(factor2 != factor)
    				printf("%ld\n", factor2);
    		}
    	}
    
    	return 0;
    }
    Uses less than sqrt(input) iterations, but the factors are out of order. (But it should be easy to order, if you know how many there are: they go smallest, biggest, 2nd-smallest, 2nd-biggest, etc, working towards a middle.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    UmeŚ, Sweden
    Posts
    555
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int n;
    	int i;
    
    	printf("Number: ");
    	scanf("%d", &n);
    
    	if (n <= 1) {
    		printf("Bad input.\n");
    		return 1;
    	}
    	for (i = 2; n != 1; i++) {
    		while (n % i == 0) {
    			printf("%d ", i);
    			n /= i;
    		}
    	}
    
    	return 0;
    }
    27 = 3 3 3
    50 = 2 5 5
    5642 = 2 7 13 31

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    So I was bored... but this time the factors are in order:

    Code:
    #include <stdio.h>
    void factorize(const int n, int last)
    {
    	for(++last; n % last; ++last)
    	{
    		if(last * last == n)
    		{
    			printf("%d\n", n);
    			return;
    		}
    		if(last * last > n)
    		{
    			return;
    		}
    	}
    	if(last * last == n)
    	{
    		printf("%d\n", last);
    		return;
    	}
    	printf("%d\n", last);
    	factorize(n, last);
    	printf("%d\n", n / last);
    }
    int main()
    {
    	int n;
    	printf("Number: ");
    	scanf("%d", &n);
    	factorize(n, 1);
    	return 0;
    }
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21