Advertisement
Garey

Oki_HappyNumbers_Newton

Apr 22nd, 2019
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. // ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <random>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11.  
  12. void generate(int *, int&);
  13. void happyNumbers(int *, int);
  14. void sets(int*, int, int*, int*);
  15. void print(int *, int);
  16.  
  17. /*! C(n, k) = C(n - 1, k - 1) + C(n - 1, k) */
  18. /*! C(n, 0) = C(n, n) = 1 */
  19. /*! C(12, 2) = 11 1 + 11 2, 10 - 0 10 1 = 66 */
  20. int newtonBinomial(int n, int k) {
  21.     if (k == 0  || k < 0|| k == n)
  22.         return 1;
  23.  
  24.     return newtonBinomial(n - 1, k - 1) + newtonBinomial(n - 1, k);
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30.  
  31.     int lotteryNumbers[100];
  32.     int distinct[100], axioation[100];
  33.     int choice, lotteryNumbersSize;
  34.  
  35.     do {
  36.  
  37.         system("cls");
  38.  
  39.         cout << "1. Generate\n";
  40.         cout << "2. Happy Numbers\n";
  41.         cout << "3. Sets\n";
  42.         cout << "4. Print\n";
  43.         cout << "5. Newton's Binomial Coefficient\n\n";
  44.  
  45.         cout << "0. Exit\n\n";
  46.  
  47.         cout << "Your choice: ";
  48.         cin >> choice;
  49.  
  50.         switch (choice) {
  51.         case 1:
  52.  
  53.             system("cls");
  54.                
  55.             generate(lotteryNumbers, lotteryNumbersSize);
  56.  
  57.             cout << "\n\nPress any key to get back to the menu..";
  58.             _getch();
  59.             break;
  60.  
  61.         case 2:
  62.  
  63.             system("cls");
  64.  
  65.             happyNumbers(lotteryNumbers, lotteryNumbersSize);
  66.  
  67.             cout << "\n\nPress any key to get back to the menu..";
  68.             _getch();
  69.             break;
  70.  
  71.         case 3:
  72.  
  73.             system("cls");
  74.  
  75.             sets(lotteryNumbers, lotteryNumbersSize, distinct, axioation);
  76.  
  77.             cout << "\n\nPress any key to get back to the menu..";
  78.             _getch();
  79.             break;
  80.  
  81.         case 4:
  82.  
  83.             system("cls");
  84.  
  85.             print(lotteryNumbers, lotteryNumbersSize);
  86.  
  87.             cout << "\n\nPress any key to get back to the menu..";
  88.             _getch();
  89.             break;
  90.  
  91.         case 5: {
  92.  
  93.             system("cls");
  94.  
  95.             int n, k;
  96.  
  97.             cout << "This is a combination of C(n, k) = C(n - 1, k - 1) + C(n - 1, k)\n\n";
  98.  
  99.             cout << "Enter n: ";
  100.             cin >> n;
  101.  
  102.             cout << "Enter k: ";
  103.             cin >> k;
  104.  
  105.             int coefficient = newtonBinomial(n, k);
  106.  
  107.             cout << "\n\nNewton's Binomial Coefficient is " << coefficient;
  108.  
  109.             cout << "\n\nPress any key to get back to the menu..";
  110.             _getch();
  111.             break;
  112.             }
  113.         }
  114.     } while (choice != 0);
  115. }
  116.  
  117. void generate(int *array, int& array_size)
  118. {
  119.     random_device rd;
  120.     mt19937 gen(rd());
  121.     uniform_int_distribution<> dist(100000, 999999);
  122.     uniform_int_distribution<> arrSize(50, 100);
  123.  
  124.     array_size = arrSize(gen);
  125.  
  126.     for (int i = 0; i < array_size; i++) {
  127.         array[i] = dist(gen);
  128.     }
  129.  
  130.     cout << "Numbers generated!\n";
  131. }
  132.  
  133. void happyNumbers(int *array, int size) {
  134.    
  135.     cout << "Happy Numbers:\n\n";
  136.  
  137.     int counter = 0;
  138.  
  139.     for (int i = 0; i < size; i++) {
  140.         string firstNumbers(to_string(array[i]).substr(0, 3));
  141.         string lastNumbers(to_string(array[i]).substr(3, 3));
  142.        
  143.         int firstSum = 0, lastSum = 0;
  144.  
  145.         for (int i = 0; i < firstNumbers.length(); i++) {
  146.             firstSum += firstNumbers[i] - 48;
  147.            
  148.             //cout << fistSum[i] << " -- ";
  149.         }
  150.  
  151.         for (int i = 0; i < lastNumbers.length(); i++) {
  152.             lastSum += lastNumbers[i] - 48;
  153.  
  154.             //cout << lastSum[i] << " -- ";
  155.         }
  156.  
  157.         if (firstSum == lastSum) {
  158.             counter++;
  159.             cout << "Number:  " << array[i] << "  - First Numbers sum: " << firstSum << " - Last Numbers sum: " << lastSum << endl;
  160.         }
  161.     }
  162.  
  163.     if (counter == 0)
  164.         cout << "There are no happy numbers!";
  165. }
  166.  
  167. void sets(int *array, int size, int *arr1, int *arr2) {
  168.     for (int i = 0; i < size; i++) {
  169.         if (((i >> 2) << 2) == i && (i & 1))
  170.             arr1[i] = i;
  171.  
  172.         if (((i >> 2) << 2) == i)
  173.             arr2[i] = i;
  174.         else if (i & 1)
  175.             arr2[i] = i;
  176.     }
  177.  
  178.     cout << "Sets have been copied to two new arrays!";
  179. }
  180.  
  181. void print(int * array, int size) {
  182.     for (int i = 0; i < size; i++) {
  183.         if (((i >> 2) << 2) == i)
  184.             cout << endl;
  185.  
  186.         cout << array[i] << "  ";
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement