26 August 2009

Sudoku


//sudoku solver
#include <iostream>
#include <time.h>
int array[9][9];
void FillSudoku();
int FindNumber(int,int);
using namespace std;
int main ()
{
// declarations
 int sum = 0;
 
 for (int i=0; i<9; i++)
 {for (int j=0; j<9; j++)
array[i][j] = FindNumber(i,j);
   }

// cout the sudoku
   for (int i=0; i<9; i++)
  {for (int j=0; j<9; j++)
  cout << array[i][j] << " "; 
cout << endl;}

// the end
system ("PAUSE");
return 0;
}

void FillSudoku()
{
    srand(time(0));
    int x;
  for (int i=0; i<9; i++)
  for (int j=0; j<9; j++)
  { do {x = rand(); } while(x>9 || x<1); 
array[i][j] = x;}
}
 
int FindNumber(int i,int j)
{ int a = 1;
    
for (int i=0; i<9; i++)
if (array[i][j]==a)
  a++; 
    
   for (int j=0; j<9; j++)
if (array[i][j]==a)
  a++; 
    
  return a;
  }

Sudoku Algorithm

// Suduku Algorithm
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int T[9][9];
//1
class find {
    public: 
void FillAll();
void Fslot(int,int);
};

class check {
 public:
bool checkAll();
bool row(int);
bool col(int);
};

void find::FillAll()
{ for (int i=0; i<9; i++)
  for (int j=0; j<9; j++)
  find::Fslot(i,j);
 
void find::Fslot(int m, int n)
{ int x = 0;
  while(true){
 x++; 
 if ((x!=T[m][0])&&(x!=T[m][1])&&(x!=T[m][2])&&(x!=T[m][3])&&(x!=T[m][4])&&(x!=T[m][5])&&(x!=T[m][6])&&(x!=T[m][7])&&(x!=T[m][8])&&(x!=T[0][n])&&(x!=T[1][n])&&(x!=T[2][n])&&(x!=T[3][n])&&(x!=T[4][n])&&(x!=T[5][n])&&(x!=T[6][n])&&(x!=T[7][n])&&(x!=T[8][n]))
       break; }
     T[m][n] = x; 
}  
 
bool check::checkAll()
{ for (int z=0; z<9; z++)
  { if (!check::row(z))
  return false;
 else if (!check::col(z))
  return false;
  }
return true;}

bool check::row(int m)
{ int sum = 0;
  for ( int i = 0; i < 9; i++ )
  sum = sum + T[m][i];
  if (sum == 45)
  return true;
     else 
        return false;
     } 
     
bool check::col(int n)
{ int sum = 0;
  for ( int i = 0; i < 9; i++ )
  sum = sum + T[i][n];
  if (sum == 45)
  return true;
     else 
        return false;
  }
 
//2
int main ()
{ int v;
  ifstream source;
  source.open("file.txt");
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
{source >> v;
T[i][j] = v; }
 
//FillAll();
find::FillAll();
if(check::checkAll())
   cout << "YES,DONE\n";
   
for (int x=0; x<9; x++)
{for (int y=0; y<9; y++)
cout << setw(3) << T[x][y];
  cout << endl;}
system ("pause");
return 0;
}
 

recursion

// Suduku Algorithm
#include <iostream>
#include <iomanip>
void FillAll();
void Fslot(int,int);
bool checkAll();
bool row(int);
bool col(int);

using namespace std;
int T[9][9];
int main ()
{
FillAll();
if(checkAll())
   cout << "YES..DONE\n";
   
for (int i=0; i<9; i++)
{for (int j=0; j<9; j++)
cout << setw(3) << T[i][j];
  cout << endl;}
system ("pause");
return 0;
}
 
void FillAll()
{ for (int i=0; i<9; i++)
  for (int j=0; j<9; j++)
  Fslot(i,j);
 
void Fslot(int m, int n)
{ int x = 0;
  while(true){
 x++; 
 if ((x!=T[m][0])&&(x!=T[m][1])&&(x!=T[m][2])&&(x!=T[m][3])&&(x!=T[m][4])&&(x!=T[m][5])&&(x!=T[m][6])&&(x!=T[m][7])&&(x!=T[m][8])&&(x!=T[0][n])&&(x!=T[1][n])&&(x!=T[2][n])&&(x!=T[3][n])&&(x!=T[4][n])&&(x!=T[5][n])&&(x!=T[6][n])&&(x!=T[7][n])&&(x!=T[8][n]))
       break; }
     T[m][n] = x; 
}  
///////////////////////////////////////////////////
// Checking Class
bool checkAll()
{ for (int z=0; z<9; z++)
  { if (!row(z))
  return false;
 else if (!col(z))
  return false;
  }
return true;}
bool row(int m)
{ int sum = 0;
  for ( int i = 0; i < 9; i++ )
  sum = sum + T[m][i];
  if (sum == 45)
  return true;
     else 
        return false;
     } 
bool col(int n)
{ int sum = 0;
  for ( int i = 0; i < 9; i++ )
  sum = sum + T[i][n];
  if (sum == 45)
  return true;
     else 
        return false;
  }
 
 
 

Primes Book1

25 August 2009

Primes

 
Ranges of 10 Prime Possibilities   
20- 32 NULL   
18- 21- 24 1   
11- 29 3   
9- 12- 30- 36- 39 7   
14 9   
28 13   
3- 6- 15- 25- 27- 33 17   
40 19   
16 37   
2- 5- 8- 17- 23- 26- 35- 37- 38 39   
34 79   
0- 4- 31 137   
7 139   
13 179   
22 379   
1- 10- 19 1379  



 








 
0 137   
1 1379   
2 39   
3 17   
4 137   
5 39   
6 17   
7 139   
8 39   
9 7   
10 1379   
11 3   
12 7   
13 179   
14 9   
15 17   
16 37   
17 39   
18 1   
19 1379   
20 0   
21 1   
22 379   
23 39   
24 1   
25 17   
26 39   
27 17   
28 13   
29 3   
30 7   
31 137   
32 0   
33 17   
34 79   
35 39   
36 7   
37 39   
38 39   
39 7   
40 19  



prime numbers

// prime numbers 
/* This is a new method to calculate the prime numbers. Get odd numbers set,
   then subtract from it the set formed from multiplying odd numbers with each other.*/   
#include <iostream>
#include <fstream>
using namespace std;

int main()
{   int a, b, c, i; 
    int array[1000];
    i = 0;
    ofstream source;
    source.open("Prime Numbers.txt"); 
    source << "This is a list of all odd numbers where nonprimes numbers are hidden and replaced by -1." << endl; 
    source << "This gives us and leave us with the Prime Numbers shown below." << endl;
    source << endl << 2 << endl << 5 << endl;
    
    for (c=1; c<2000; c+=2)
        if (c%5==0)
            {array[i] = 0;i++;}
        else
            {array[i] = c;i++;}
            
    for (a=3; a<1000; a+=2)
        for (b=a; b<1000; b+=2)
            for (i=0; i<1000; i++)
                if ( a*b == array[i])
                     array[i] = -1;
    
    for (i=0; i<1000; i++)
        if (i%5==0)
            source << endl << "........\n" << array[i] << endl;   
        else
            source << array[i] << endl;                                   
        
    system("PAUSE");
    return 0;
    }

Prims list


//Create Prime list on a file
//Author: Ahmed Sayed
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
  int num;
  cout << "Enter Range ";
  ofstream source;
  source.open("Prime List.txt");
cin >> num; 
 for (int x=1; x<num; x++)
{
  bool flag = true;
 
  for (int i = 2; i < x; i++)
  if (x%i==0)
    flag = false;
    
   if (flag)
  source << x << endl;
  }
  
system ("pause");
return 0;
}

Is Prim

//Is Prime
//Author: Ahmed Sayed
#include <iostream>
using namespace std;

int main ()
{
  int num;
  do {
  cout << "Enter a number for checking or '0' to exit:_ "; cin >> num;
  bool flag = true;
  for (int i=2; i<=num/2; i++)
  if (num%i==0)
    flag = false;
   if (flag)
  cout << "Prim!\n" << endl;
     else
  cout << "Not Prim\n
  " << endl; }while (num!=0); 
  
system ("pause");
return 0;
}

12 May 2009

bubble sorting algorithm

// this program is to sort words from a file ascending or descending by bubble sorting algorithm
//0
#include <iostream>
#include <string>
#include <fstream>
//1
using namespace std;
//2
int main ()
{
// declare   
  string a[21000];  // define huge array
  string s = "";
  int k, z = 0;
// import all the words in an array 
  ifstream word;
  word.open("words.txt");
  while (! word.eof()) 
      {
            word >> a[z]; // insert new word; do z++
            z++;
      };
// sort the array using bubble sorting algorithm
    for (int i = 0; i < z; i++)
        {
            for ( k = 1; k < (z - i); k++) // each word we do n-itsPosition comparisons
            {
                if (a[i+k] < a[i])    // for ascending use <; for descending use >
                 {
                    s = a[i];
                    a[i] = a[i+k];
                    a[i+k] = s;
                  }
            }
        }
             
// print out the sorted words
    for (int j = 0; j < z; j++)     // z is number of lines in file
        cout << a[j] << endl; 
// pause
    system("PAUSE");   
    return 0;
}