My 1st Try On Code Blog

Trying to code a program to find PRIMES in a complex way:

#include <iostream>
#include <cmath>
using namespace std;
int prime(int x) {//if a is prime, then 1; else, then 0;
    int k;
    for (k = 2;k < sqrt(x);k++){
        if (x % k == 0) break;
}
    if (sqrt(x) < k) return 1;
    else return 0;
}
void printPrime(int num) {//print prime
    if (prime(num)) cout << num << endl;
    else;
}
int main(void) {
    int num;
    cin >> num;
    cout << "\nThe following are the Primes less than " << num << ": \n\n";
    for (int i = 2;i < num;i++) {
        printPrime(i);
    }

    return 0;
}

 

The Euclid's algorithm, the result shows how does the modular operation take place:

#include<iostream>
using namespace std;
int main(void) {
    int a, b, c;
    //Euclid's algorithm
    cin >> a >> b;
    for (int i = 1;;i++) {
        //to show the how many times we are processing
        cout << "THE " << "TEST" << i << endl;

        //the Euclid's algorithm
        c = a % b;
        cout << "c = a % b " << "  c == " << c << endl;/*
        a node to help us to understand better*/

        a = b;
        cout << "a = b " << "      a == " << a << endl;/*
        a node to help us to understand better*/

        b = c;
        cout << "b = c " << "      b == " << b << endl << endl;/*
        a node to help us to understand better*/

        if (c == 0) break;
    }
    cout << a;

    return 0;
}

 

 转发请注明来源,谢谢!

posted @ 2020-11-25 18:58  VVEN2014  阅读(52)  评论(0)    收藏  举报