Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <random>
- #include <conio.h>
- using namespace std;
- void generate(int *, int&);
- void happyNumbers(int *, int);
- void sets(int*, int, int*, int*);
- void print(int *, int);
- /*! C(n, k) = C(n - 1, k - 1) + C(n - 1, k) */
- /*! C(n, 0) = C(n, n) = 1 */
- /*! C(12, 2) = 11 1 + 11 2, 10 - 0 10 1 = 66 */
- int newtonBinomial(int n, int k) {
- if (k == 0 || k < 0|| k == n)
- return 1;
- return newtonBinomial(n - 1, k - 1) + newtonBinomial(n - 1, k);
- }
- int main()
- {
- int lotteryNumbers[100];
- int distinct[100], axioation[100];
- int choice, lotteryNumbersSize;
- do {
- system("cls");
- cout << "1. Generate\n";
- cout << "2. Happy Numbers\n";
- cout << "3. Sets\n";
- cout << "4. Print\n";
- cout << "5. Newton's Binomial Coefficient\n\n";
- cout << "0. Exit\n\n";
- cout << "Your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- generate(lotteryNumbers, lotteryNumbersSize);
- cout << "\n\nPress any key to get back to the menu..";
- _getch();
- break;
- case 2:
- system("cls");
- happyNumbers(lotteryNumbers, lotteryNumbersSize);
- cout << "\n\nPress any key to get back to the menu..";
- _getch();
- break;
- case 3:
- system("cls");
- sets(lotteryNumbers, lotteryNumbersSize, distinct, axioation);
- cout << "\n\nPress any key to get back to the menu..";
- _getch();
- break;
- case 4:
- system("cls");
- print(lotteryNumbers, lotteryNumbersSize);
- cout << "\n\nPress any key to get back to the menu..";
- _getch();
- break;
- case 5: {
- system("cls");
- int n, k;
- cout << "This is a combination of C(n, k) = C(n - 1, k - 1) + C(n - 1, k)\n\n";
- cout << "Enter n: ";
- cin >> n;
- cout << "Enter k: ";
- cin >> k;
- int coefficient = newtonBinomial(n, k);
- cout << "\n\nNewton's Binomial Coefficient is " << coefficient;
- cout << "\n\nPress any key to get back to the menu..";
- _getch();
- break;
- }
- }
- } while (choice != 0);
- }
- void generate(int *array, int& array_size)
- {
- random_device rd;
- mt19937 gen(rd());
- uniform_int_distribution<> dist(100000, 999999);
- uniform_int_distribution<> arrSize(50, 100);
- array_size = arrSize(gen);
- for (int i = 0; i < array_size; i++) {
- array[i] = dist(gen);
- }
- cout << "Numbers generated!\n";
- }
- void happyNumbers(int *array, int size) {
- cout << "Happy Numbers:\n\n";
- int counter = 0;
- for (int i = 0; i < size; i++) {
- string firstNumbers(to_string(array[i]).substr(0, 3));
- string lastNumbers(to_string(array[i]).substr(3, 3));
- int firstSum = 0, lastSum = 0;
- for (int i = 0; i < firstNumbers.length(); i++) {
- firstSum += firstNumbers[i] - 48;
- //cout << fistSum[i] << " -- ";
- }
- for (int i = 0; i < lastNumbers.length(); i++) {
- lastSum += lastNumbers[i] - 48;
- //cout << lastSum[i] << " -- ";
- }
- if (firstSum == lastSum) {
- counter++;
- cout << "Number: " << array[i] << " - First Numbers sum: " << firstSum << " - Last Numbers sum: " << lastSum << endl;
- }
- }
- if (counter == 0)
- cout << "There are no happy numbers!";
- }
- void sets(int *array, int size, int *arr1, int *arr2) {
- for (int i = 0; i < size; i++) {
- if (((i >> 2) << 2) == i && (i & 1))
- arr1[i] = i;
- if (((i >> 2) << 2) == i)
- arr2[i] = i;
- else if (i & 1)
- arr2[i] = i;
- }
- cout << "Sets have been copied to two new arrays!";
- }
- void print(int * array, int size) {
- for (int i = 0; i < size; i++) {
- if (((i >> 2) << 2) == i)
- cout << endl;
- cout << array[i] << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement