feat(c++): pascals' triangle in c++ and babylonian sqrt
-
This commit is contained in:
parent
497f86d30c
commit
136cd9adf7
2 changed files with 86 additions and 0 deletions
21
languages/c++/babylonian.cc
Normal file
21
languages/c++/babylonian.cc
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int sqrt(int number) {
|
||||||
|
int initialGuess = 2;
|
||||||
|
|
||||||
|
while (abs(initialGuess - number / initialGuess) > 1) {
|
||||||
|
initialGuess = (initialGuess + number / initialGuess) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return initialGuess;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cout << "Type a number to predict the square root of:";
|
||||||
|
|
||||||
|
int number;
|
||||||
|
cin >> number;
|
||||||
|
|
||||||
|
cout << sqrt(number);
|
||||||
|
}
|
65
languages/c++/pascal.cc
Normal file
65
languages/c++/pascal.cc
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Calculate Pascal's triangle
|
||||||
|
vector<vector<int>> pascal(int rowCount) {
|
||||||
|
vector<vector<int>> rows{{1}};
|
||||||
|
|
||||||
|
for (int i = 0; i < rowCount; i++) {
|
||||||
|
vector<int> previousRow = rows[rows.size() - 1];
|
||||||
|
vector<int> newRow{1};
|
||||||
|
|
||||||
|
for (int j = 0; j < previousRow.size() - 1; j++) {
|
||||||
|
newRow.push_back(previousRow[j] + previousRow[j + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
newRow.push_back(1);
|
||||||
|
rows.push_back(newRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
int vectorStringLength(vector<int> vector) {
|
||||||
|
int value = vector.size();
|
||||||
|
|
||||||
|
for (int i = 0; i < vector.size(); i++) {
|
||||||
|
value += to_string(vector[i]).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
vector<vector<int>> pascalsTriangle = pascal(10);
|
||||||
|
int bottomRowSize = vectorStringLength(pascalsTriangle[pascalsTriangle.size() - 1]);
|
||||||
|
|
||||||
|
// Output the triangle
|
||||||
|
for (int i = 0; i < pascalsTriangle.size(); i++) {
|
||||||
|
vector<int> currentRow = pascalsTriangle[i];
|
||||||
|
int rowSize = vectorStringLength(currentRow);
|
||||||
|
int sizeDifference = bottomRowSize - rowSize;
|
||||||
|
string spacing = "";
|
||||||
|
|
||||||
|
for (int j = 0; j < sizeDifference / 2; j++) {
|
||||||
|
spacing += " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j < currentRow.size(); j++) {
|
||||||
|
if (j == 0) {
|
||||||
|
cout << spacing;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << to_string(currentRow[j]) + " ";
|
||||||
|
|
||||||
|
if (j == currentRow.size() - 1) {
|
||||||
|
cout << spacing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i != pascalsTriangle.size() - 1) {
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue