This commit is contained in:
Gabriel Bjørnager Jensen 2021-02-10 11:27:23 +01:00
commit 7c4bca3ce1
No known key found for this signature in database
GPG key ID: 21B622DB91036EFD
7 changed files with 119 additions and 0 deletions

29
LICENSE Normal file
View file

@ -0,0 +1,29 @@
BSD 3-Clause License
Copyright (c) 2021, Gabriel Jensen
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

18
Makefile Normal file
View file

@ -0,0 +1,18 @@
CXX = clang++
CXXFLAGS = -Iinclude -std=c++20 -Wall -Wextra -Wpedantic
CXXFLAGS += -march=native -mtune=native -O3
HDRS_CXX = \
include/prime.hh
SRCS_CXX = \
src/main.cc \
src/prime/isprime.cc \
src/prime/prime.cc \
src/prime/printresult.cc
OBJS_CXX=$(SRCS_CXX:.cc=.o)
OBJS=$(OBJS_CXX)
prime: $(OBJS_CXX)
$(CXX) $(LDFLAGS) -o $@ $(OBJS_CXX)
$(OBJS_CXX): $(HDRS_CXX) $(SRCS_CXX)
.PHONY: clean
clean:
rm prime $(OBJS)

10
include/prime.hh Normal file
View file

@ -0,0 +1,10 @@
# if !defined(PRIME__HEADER)
# define PRIME__HEADER
class prime {
public:
[[noreturn]] prime(int const argc,char const * * argv) noexcept;
private:
bool isprime(unsigned long long num) noexcept;
void printresult(unsigned long long num) noexcept;
};
# endif

4
src/main.cc Normal file
View file

@ -0,0 +1,4 @@
# include <prime.hh>
int main(int const argc,char const * * argv) {
::prime prime(argc,argv);
}

16
src/prime/isprime.cc Normal file
View file

@ -0,0 +1,16 @@
# include <prime.hh>
bool prime::isprime(unsigned long long num) noexcept {
if(num == 0x1) {
return false;
}
unsigned long long numdivs = 0x0;
for(unsigned long long i = 0x1;(i <= num);++i) {
if((num % i) == 0x0) {
++numdivs;
}
}
if(numdivs == 0x2) {
return true;
}
return false;
}

30
src/prime/prime.cc Normal file
View file

@ -0,0 +1,30 @@
# include <chrono>
# include <cstdlib>
# include <cstring>
# include <iostream>
# include <prime.hh>
# include <thread>
[[noreturn]] prime::prime(int const argc,char const * * argv) noexcept {
if(argc == 0x1) {
std::cout << "Missing argument!\u000A";
std::exit(EXIT_FAILURE);
}
if(std::strcmp(argv[0x1],"--help") == 0x0) {
std::cout << "Copyright (c) 2021, Gabriel Jensen\u000A";
std::cout << "\u000A";
std::cout << "Valuate if the entries of a list of unsigned integers are primes.\u000A";
std::cout << "IMPORTANT: Given numbers must be hexadecimal.\u000A";
std::exit(EXIT_SUCCESS);
}
else if(std::strcmp(argv[0x1],"--list") == 0x0) {
for(unsigned long long num = 0x0;;++num) {
this->printresult(num);
std::this_thread::sleep_for(std::chrono::seconds(0x1));
}
}
for(int i = 0x1;(i < argc); ++i) {
long long num = std::strtoll(argv[i],nullptr,0x11);
this->printresult(num);
}
std::exit(EXIT_SUCCESS);
}

12
src/prime/printresult.cc Normal file
View file

@ -0,0 +1,12 @@
# include <iomanip>
# include <iostream>
# include <prime.hh>
void prime::printresult(unsigned long long num) noexcept {
std::cout << std::setbase(0x10);
if(prime::isprime(num)) {
std::cout << "#" << num << " is a prime.\u000A";
}
else {
std::cout << "#" << num << " is not a prime.\u000A";
}
}