This C++ Program which generates a sequence of ulam numbers. Ulam number is defined to be the smallest integer that is the sum of two distinct earlier terms in exactly one way. The program uses 1 and 2 as the first two terms and the other terms are generated on the basis of definition of ulam numbers and stored in a vector.
Here is source code of the C++ program which generates a sequence of ulam numbers. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Generate Sequence of Ulam Numbers
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int uterm1 = 1, uterm2 = 2, num = 0;
vector<int> v;
v.push_back(1);
num++;
v.push_back(2);
num++;
for (int i = 3; i <= 100; i++)
{
int count = 0;
for (int j = 0; j < num; j++)
{
for (int k = j + 1; k < num; k++)
{
if (v[j] + v[k] == i)
count++;
}
}
if (count == 1){
v.push_back(i);
num++;
}
}
cout << "The sequence of ulam numbers is as follows : " << endl;
for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i)
cout << *i << '\t';
cout << endl;
}
$ g++ main.cpp $ ./a.out The sequence of ulam numbers is as follows : 1 2 3 4 6 8 11 13 16 18 26 28 36 38 47 48 53 57 62 69 72 77 82 87 97 99
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check C++ Books
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check Computer Science Books
- Check Programming Books