[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include(/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php) [function.include]: failed to open stream: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include() [function.include]: Failed opening '/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php' for inclusion (include_path='.:/opt/php53/lib/php')
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include(/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php) [function.include]: failed to open stream: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include() [function.include]: Failed opening '/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php' for inclusion (include_path='.:/opt/php53/lib/php')
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include(/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php) [function.include]: failed to open stream: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 122: include() [function.include]: Failed opening '/home/shoeb/public_html/www.matholympiad.org.bd/forum/includes/phpbb-latex.php' for inclusion (include_path='.:/opt/php53/lib/php')
[phpBB Debug] PHP Warning: in file [ROOT]/includes/session.php on line 1042: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4786: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4788: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4789: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4790: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3887)
BdMO Online Forum • View topic - Count The Number Of Solutions

Count The Number Of Solutions

Discuss Computer Science and Programming related problems

Moderators: bristy1588, Labib

Facebook Twitter

Count The Number Of Solutions

Post Number:#1  Unread postby Fahim Shahriar » Sat Aug 10, 2013 2:26 pm

Suppose, I want to find the number of solutions of this equation $a+b+c+d=7$ ; where $a,b,c,d$ are natural numbers.

To find the solutions I wrote the following code.

Code: Select all
#include <stdio.h>

int main ()
{
  int a,b,c,d,n;
  for(a=1;a>=0 && a<=7;a++)
  {for(b=1;b>=0 && b<=7;b++)
  {for(c=1;c>=0 && c<=7;c++)
  {for(d=1;d>=0 && d<=7;d++)
  {
      n = a + b + c + d;
  if(n==7)
  {
          printf("%d %d %d %d\n",a,b,c,d);         
  }}}}}
  system("pause");
  return 0;
}


Now, add something to the code that will count the number of solutions.
Name: Fahim Shahriar Shakkhor
Notre Dame College
User avatar
Fahim Shahriar
 
Posts: 138
Joined: Sun Dec 18, 2011 12:53 pm

Re: Count The Number Of Solutions

Post Number:#2  Unread postby rakeen » Sat Aug 10, 2013 10:52 pm

Just add a counter!
Code: Select all
int main ()
{
  int a,b,c,d,n,count=0;
  for(a=1;a>=0 && a<=7;a++){
     for(b=1;b>=0 && b<=7;b++){
        for(c=1;c>=0 && c<=7;c++){
           for(d=1;d>=0 && d<=7;d++){
               n = a + b + c + d;
              if(n==7){
                 count++;
                   printf("%d %d %d %d\n",a,b,c,d);         
              }
           }
        }
     }
  }
  printf("%d",count);
  system("pause");
  return 0;
}
r@k€€/|/
User avatar
rakeen
 
Posts: 384
Joined: Thu Dec 09, 2010 5:21 pm
Location: Dhaka

Re: Count The Number Of Solutions

Post Number:#3  Unread postby Masum » Tue Jan 07, 2014 1:52 pm

You can optimize this code. For a silly example, you don't need the condition $a>=0$ and $a<= 7$ at a time since you are starting the loop from $1$, there is no reason for $a$ to be $0$.
One one thing is neutral in the universe, that is $0$.
User avatar
Masum
 
Posts: 592
Joined: Tue Dec 07, 2010 1:12 pm
Location: Dhaka,Bangladesh


Share with your friends: Facebook Twitter

  • Similar topics
    Replies
    Views
    Author

Return to Computer Science

Who is online

Users browsing this forum: No registered users and 1 guest