0 Replies - 476 Views - Last Post: 19 August 2010 - 01:44 PM Rate Topic: -----

#1 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Find all three digit complete couple numbers

Posted 19 August 2010 - 01:44 PM

Description: I wrote using vs 2010 on an xp system. This program will find all 3 digit numbers that are complete couples.A complete couple number is equal to the sum of all unique couplings of its digits.

For example one complete couple would be 132. This number when rearranged by couples yields the following unique pairs:
13+12+31+32+21+23 = 132.
//Written by Julian Apodaca
//August 19, 2010


#include <stdio.h>
#include <stdlib.h>

int getSum(int number);

int main()
{
	int i;

	for(i = 100; i < 1000; i++)
	{
		if (getSum(i) == i)
		{
			printf("%dn", getSum(i));
		}
	}
	fflush(stdout);
	(void)getchar();
}

int getSum(int number)
{
	int i=0,j=1,k=2;
	int digit1 = 0;
	int digitArray[3];

	//these char arrays are used to combine each int array to a char number
	char pair12[3],pair13[3],pair21[3];
	char pair23[3],pair31[3],pair32[3];
	
	//create an array of the number being tested
	for(i = 0; i < 3; i++)
	{	
		digitArray[k--] = number % 10;
		number = number/10;
	}
	k = 2;
	//create all possible couples then convert to a char array
	for(i = 0; i<2;i++)
	{	
		sprintf(&pair12[i], "%d", digitArray[i]);
		sprintf(&pair13[i], "%d", digitArray[i+i]);
		sprintf(&pair21[i], "%d", digitArray[j-i]);
		sprintf(&pair23[i], "%d", digitArray[j+i]);
		sprintf(&pair31[i], "%d", digitArray[(k-(k*i))]);
		sprintf(&pair32[i], "%d", digitArray[k-i]);	
	}

	//add all pairings of the numbers
	return atoi(pair12) + atoi(pair13) + atoi(pair21) + atoi(pair23) 
	+atoi(pair31) + atoi(pair32);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Find all three digit complete couple numbers

#2 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: Find all three digit complete couple numbers

Posted 19 August 2010 - 01:44 PM

Description: Compiled in linux and should work just fine in windows.This program will find all 3 digit numbers that are complete couples.A complete couple number is equal to the sum of all unique couplings of its digits.

For example one complete couple would be 132. This number when rearranged by couples yields the following unique pairs:
13+12+31+32+21+23 = 132.
//Written by Julian Apodaca
//August 19, 2010

#include <stdio.h>
#include <stdlib.h>

int getSum(int number);

int main(void)
{
  int i;

  for(i = 100; i < 1000; i++)
  {
    if (getSum(i) == i)
    {
      printf("%dn", getSum(i));
    }
  } 
  return 0;
}

int getSum(int number)
{
  int i=0,j=1,k=2;
  int digitArray[3];

  //these char arrays are used to combine each int array to a char number
  char pair12[3],pair13[3],pair21[3];
  char pair23[3],pair31[3],pair32[3];
	
  //create an array of the number being tested
  for(i = 0; i < 3; i++)
  {	
    digitArray[k--] = number % 10;
    number = number/10;
  }
  k = 2;
  //create all possible couples then convert to a char array
  for(i = 0; i<2;i++)
  {	
    sprintf(&pair12[i], "%d", digitArray[i]);
    sprintf(&pair13[i], "%d", digitArray[i+i]);
    sprintf(&pair21[i], "%d", digitArray[j-i]);
    sprintf(&pair23[i], "%d", digitArray[j+i]);
    sprintf(&pair31[i], "%d", digitArray[(k-(k*i))]);
    sprintf(&pair32[i], "%d", digitArray[k-i]);	
  }

  //add all pairings of the numbers
  return atoi(pair12) + atoi(pair13) + atoi(pair21) + atoi(pair23) 
    +atoi(pair31) + atoi(pair32);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1