Thread: cutting output question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    cutting output question..

    Ok ,like you said I mechanickly created each of the pruposed function of my C++
    code

    but i need to cut it down so for each number it will print the pair with
    the smallest number
    for 54(showes now):
    24=12+12
    30=12+18
    32=12+20
    36=12+24
    36=18+18
    38=18+20
    40=20+20
    42=12+30
    42=18+24
    44=20+24
    48=12+36
    48=18+30
    48=24+24
    50=20+30
    52=12+40
    54=12+42
    54=18+36
    54=24+30

    i need it to show for 54(for each number i need only one sum of the smallest number pair):
    54=12+42
    52=12+40
    50=20+30
    48=12+36
    44=20+24
    42=12+30
    40=20+20
    38=18+20
    36=12+24
    24=12+12
    30=12+18
    32=12+20

    Code:
    #include<stdio.h>
    
    int sum_of_divisors(int n) {
    	
    	int result=1;
    
    	int k=2;
    	for(k=2;k*k<=n;++k) { 
        if(n%k==0) { 
          int sum_of_powers=1, power=1; 
          do { 
            power*=k; 
            sum_of_powers+=power; 
            n/=k; 
          } while(n%k==0); 
          result*=sum_of_powers; 
        } 
      } 
    	if (n > 1)
        return (n + 1) * result;
    else
        return result;
    }
    
    int is_abundant(int tndex) {
    int jndex,sum;
    int flag_1;
    flag_1=0;
    sum=0;
    for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
                            if (tndex%jndex==0){
                                sum=sum+jndex;
                            }
                            if (tndex<sum){
                            flag_1=1;
                        }
    
                        }//end abbondence check for first num
    
     return flag_1;
    }
    
    void print_if_sum_of_abundants(int n) { 
     int i;
    	for( i=1;i*2<=n;++i) {
    		if(is_abundant(i)&&is_abundant(n-i)) { 
    
    			printf("%d=%d+%d\n",n,i,(n-i));
    } 
      } 
    } 
    
    int main() { 
      int limit = 54;
    int i;
       
      
      
      for(i=1;i<=limit;++i) {
        print_if_sum_of_abundants(i); 
      }
      return 0;
    }

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I found a way by ending the loop on the first time it find that the pair is fit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cutting a list in half question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 03-22-2009, 03:35 AM
  2. Question: Output of data.txt from the following program
    By outlaw525 in forum C Programming
    Replies: 9
    Last Post: 06-23-2008, 03:33 PM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  4. a question about file output
    By wang8442 in forum C Programming
    Replies: 3
    Last Post: 12-26-2004, 03:18 AM
  5. a newbie question about file output
    By maybe in forum C Programming
    Replies: 8
    Last Post: 10-12-2004, 08:14 PM