How many solutions (using only nonnegative integers) are there to the following equation?
x1 + x2 + x3 + x4 + x5 = 34
1
LVL 17

Comment

by:Leonidas Dosas
One....x1 + x2 + x3 + x4 + x5 = 34=> x15=34=>x=34/15
0
LVL 12

Comment

by:Anwar Saiah
You got 5 solutions of this form (0,0,0,0,34) , (0,0,0,34,0)..
and then you got this form (0,0,0,1,33) that has
33*2*(5 nCr 2)=66*(5!/(2!*3!)=66*(10)=660
and then you got this form (0,0,1,32,1) ...
and so on ..

 So you got a lot of answers :)
0
LVL 16

Comment

by:MURUGESAN N
Answer count depends on
1. data type you are using.
2. related programming language
3. compiler whether 32 bit or 64 bit
Here goes related program and output I have done using cygwin at Windows.
#include <sys/errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <iostream>
using namespace std;
struct Required_Data_ThirtyFour
{
	char CurrentAnswer[1024];
	struct Required_Data_ThirtyFour *prev;
	struct Required_Data_ThirtyFour *next;
	Required_Data_ThirtyFour();
	~Required_Data_ThirtyFour();
};
struct Required_Data_ThirtyFour *CurAnsNode = NULL;
struct Required_Data_ThirtyFour *HeadNode = NULL;
struct Required_Data_ThirtyFour *TailNode = NULL;
char SearchIfAnswerPresent( char *CurrentAnswer)
{
	if ( NULL == CurrentAnswer)
	{
		cout << "CurrentAnswer is NULL\n";
		exit(0);
	}
	Required_Data_ThirtyFour *CurAnsNode = HeadNode;
	while ( CurAnsNode)
	{
		if ( 0 == strcmp( CurrentAnswer, CurAnsNode->CurrentAnswer))
		{
			return 'y';
		}
		CurAnsNode = CurAnsNode->next;
	}
		return 'n';
}
Required_Data_ThirtyFour::Required_Data_ThirtyFour()
{
	prev=NULL;
	next=NULL;
	CurrentAnswer[0]='\0';
}
Required_Data_ThirtyFour::~Required_Data_ThirtyFour()
{
}
int main()
{
	// In this program unsigned int being four bytes.
	unsigned int x1 = 0;
	unsigned int x2 = 0;
	unsigned int x3 = 0;
	unsigned int x4 = 0;
	unsigned int x5 = 0;
	unsigned int SolutionCount = 0;
	char CurrentAnswer[1024] = {};
	HeadNode = CurAnsNode = new Required_Data_ThirtyFour();
	while ( 35 >= x5)
	{
		CurAnsNode->next = new Required_Data_ThirtyFour();
		if ( NULL == CurAnsNode->next)
		{
			cout << strerror(errno) << "\n";
			exit(1);
		}
		CurAnsNode->next->prev=CurAnsNode;
		TailNode=CurAnsNode=CurAnsNode->next;
		x4 = 0;
		while ( 35 >= x4)
		{
			x3 = 0;
			while ( 35 >= x3)
			{
				x2=0;
				while ( 35 >= x2)
				{
					x1 = 0;
					while ( 35 >= x1)
					{
						if ( 34 == ( x1 + x2 + x3 + x4 + x5))
						{
							sprintf( CurrentAnswer, "%02d+%02d+%02d+%02d+%02d=%02d", x1, x2, x3, x4, x5, x1+x2+x3+x4+x5);
							char CurNodeWasSaved = SearchIfAnswerPresent( CurrentAnswer);
							if ( 'n' == CurNodeWasSaved)
							{
								strcpy( CurAnsNode->CurrentAnswer, CurrentAnswer);
								SolutionCount++;
							}
							else
							{
								break;
							}
							cout << CurAnsNode->CurrentAnswer  << "\n";
						}
						x1++;
					}
					x2++;
				}
				x3++;
			}
			x4++;
		}
		x5++;
	}
	Required_Data_ThirtyFour *CurAnsNode = TailNode;
	if ( CurAnsNode )
	{
		TailNode=TailNode->prev;
		delete CurAnsNode;
		CurAnsNode = TailNode;
	}
	// Output from last 3 lines
	// 00+00+01+00+33=34
	// 00+00+00+01+33=34
	// 00+00+00+00+34=34
	// Programmatically (C++ or C), you can
	// Replace:
	//		00
	// With:
	//		UINT32_MAX+1
	// OR
	// Replace:
	//		01
	// With:
	//		UINT32_MAX+2
	// OR
	// ...
	// Replace:
	//		34
	// With:
	//		UINT32_MAX+35
	// Value for UINT32_MAX : 4294967295
	cout << "Total answer count: " << SolutionCount;
	return 0;
}

Open in new window

Related command:
$ /usr/bin/g++ -g -Wall Non_Negative_Int_Sum.cpp -o a.out
$ ./a.out > Non_Negative_Int_Sum.txt

Open in new window

Attaching related Output.
0
LVL 17

Comment

by:Leonidas Dosas
The syntax then its wrong.It suppose to be x+y+z+v+w=34
0
LVL 16

Comment

by:MURUGESAN N
Non_Negative_Int_Sum.txt
Total answer count: 73815
Example:
Count1:            34+00+00+00+00=34
Count51993:   02+03+14+06+09=34
Count73815:   00+00+00+00+34=34
0
LVL 17

Comment

by:Leonidas Dosas
JS code

var x,y,z,v,w;
var count=0;
for(var x=0;x<=34;x++){
    for(var y=0;y<=34;y++){
       for(var z=0;z<=34;z++){
          for(var v=0;v<=34;v++){
            for(var w=0;w<=34;w++){
               if(x+y+z+v+w===34){
                 count++;
               } 
            }
            
          }
       }
    }
}
console.log(count);

Open in new window


Result 73815 solutions
0
LVL 16

Comment

by:MURUGESAN N
Related update to the comment from Leonidas Dosas.
<HTML>
	<HEAD>
		<TITLE>
			Sum solution for 34 using nonnegative integers
		</TITLE>
	</HEAD>
	<BODY>
		<TABLE border="1" height="100%" width="5%">
			<TR>
				<TD valign=top align=left>
					x1
				</TD>
				<TD valign=top align=left>
					x2
				</TD>
				<TD valign=top align=left>
					x3
				</TD>
				<TD valign=top align=left>
					x4
				</TD>
				<TD valign=top align=left>
					x5
				</TD>
				<TD valign=top align=left>
					Sum
				</TD>
				<TD valign=top align=left>
					Answer count
				</TD>
			</TR>
			<script language="javascript">
				var x=0,y=0,z=0,v=0,w=0;
				var AnswerCount=0;
				for(var x=0;x<=34;x++)
				{
					for(var y=0;y<=34;y++)
					{
						for(var z=0;z<=34;z++)
						{
							for(var v=0;v<=34;v++)
							{
								for(var w=0;w<=34;w++)
								{
									if(x+y+z+v+w===34)
									{
										document.write("<TR>");
										document.write("<TD valign=top align=left>");
										CurVar=""+x;
										if ( 1 == CurVar.length)
										{
											document.write("0"+x);
										}
										else
										{
											document.write(x);
										}
										document.write("</TD><TD valign=top align=left>");
										if ( 1 == (""+y).length)
										{
											document.write("0"+y);
										}
										else
										{
											document.write(y);
										}
										document.write("</TD><TD valign=top align=left>");
										if ( 1 == (""+z).length)
										{
											document.write("0"+z);
										}
										else
										{
											document.write(z);
										}
										document.write("</TD><TD valign=top align=left>");
										if ( 1 == (""+v).length)
										{
											document.write("0"+v);
										}
										else
										{
											document.write(v);
										}
										document.write("</TD><TD valign=top align=left>");
										if ( 1 == (""+w).length)
										{
											document.write("0"+w);
										}
										else
										{
											document.write(w);
										}
										AnswerCount++;
										document.write("</TD><TD valign=top align=left>=34</TD><TD align=left valign=top>"+AnswerCount+"</TD>");
										document.write("</TR>");
									}
								}
							}
						}
					}
			}
			document.write("<TR>");
			document.write("<TD valign=top align=left colspan=7>");
			document.write("Total answer count: "+AnswerCount);
			document.write("</TD>");
			document.write("</TR>");
			</script>
		</TABLE>
	</BODY>
</HTML>

Open in new window

Hence informing the following:
Answer count depends on
1. data type you are using.
2. related programming language
3. compiler whether 32 bit or 64 bit
4. related applications(either a.out or program.exe or java or browser or ...)
0
LVL 12

Comment

by:Anwar Saiah
This is a math problem how is your pc specs related!?
0

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month