Last active
May 13, 2024 06:29
-
-
Save sdkfz181tiger/2a0e7e5df9c92c5124ab81d94c474504 to your computer and use it in GitHub Desktop.
C/C++課題ネタ01_割り切れる数、最大公約数、最小公倍数、素数判定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// HowTo C++ | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
int main(){ | |
// C++ version... | |
if(__cplusplus == 201703L) printf("C++17\n"); | |
else if(__cplusplus == 201402L) printf("C++14\n"); | |
else if(__cplusplus == 201103L) printf("C++11\n"); | |
else if(__cplusplus == 199711L) printf("C++98\n"); | |
else printf("pre-standard C++\n"); | |
const char* name = "Shimeji"; | |
printf("Hello, %s!!\n", name); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// 3と5で割り切れる数値の合計を求める | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
int main(){ | |
printf("Hello, Cpp!!\n"); | |
// 3と5で割り切れる数値の合計を求める | |
int limit = 1000; | |
long long total = 0; | |
for(int i=3; i<limit; i++){ | |
if(i%3 == 0 || i%5 == 0) total+= i; | |
} | |
printf("Total: %llu\n", total); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// ユークリッドの互除法(最大公約数) | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
int gcd(const int a, const int b){ | |
return (b==0) ? a:gcd(b, a%b); | |
} | |
int main(){ | |
printf("Hello, Cpp!!\n"); | |
// ユークリッドの互除法(最大公約数) | |
const int a = 1024; | |
const int b = 980; | |
const int result = gcd(a, b); | |
printf("gcd: %d\n", result); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// ユークリッドの互除法(最小公倍数) | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
int gcd(const int a, const int b){ | |
return (b==0) ? a:gcd(b, a%b); | |
} | |
int lcm(const int a, const int b){ | |
return (a * b) / gcd(a, b); | |
} | |
int main(){ | |
printf("Hello, Cpp!!\n"); | |
// ユークリッドの互除法(最小公倍数) | |
const int a = 46; | |
const int b = 22; | |
const int result = lcm(a, b); | |
printf("lcm: %d\n", result); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// 素数判定 | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
bool isPrime(const int num){ | |
if(num%2 == 0) return false; | |
const int root = sqrt(num); | |
for(int i=3; i<root; i+=2){ | |
if(num%i == 0) return false; | |
} | |
return true; | |
} | |
int main(){ | |
printf("Hello, Cpp!!\n"); | |
// 素数判定 | |
const int limit = 100; | |
printf("Prime: 2"); | |
for(int i=3; i<limit; i++){ | |
if(isPrime(i)) printf(",%d", i); | |
} | |
printf("\n"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// 素数判定(双子素数) | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
bool isPrime(const int num){ | |
if(num%2 == 0) return false; | |
const int limit = sqrt(num); | |
for(int i=3; i<limit; i+=2){ | |
if(num%i == 0) return false; | |
} | |
return true; | |
} | |
int main(){ | |
printf("Hello, Cpp!!\n"); | |
// 素数判定(双子素数) | |
// 双子素数: 差が2の二つの素数 | |
// いとこ素数: 差が4の二つの素数 | |
const int limit = 100; | |
for(int i=3; i<limit; i++){ | |
if(isPrime(i) && isPrime(i+2)){ | |
printf("Primes(Twin): %d, %d\n", i, i+2); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment