Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active May 13, 2024 06:30
Show Gist options
  • Save sdkfz181tiger/d24c4cc4b6ac9887c85d0dfd810aab4b to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/d24c4cc4b6ac9887c85d0dfd810aab4b to your computer and use it in GitHub Desktop.
C/C++課題ネタ02_過剰数、友愛数、アームストロング数、素因数分解、グレイコード
//==========
// 過剰数か判定する
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <stdio.h>
int tpd(const int num){
int result = 1;
const int limit = sqrt(num);
for(int i=2; i<=limit; i++){
if(num%i != 0) continue;
result += (i==(num/i)) ? i:(i+num/i);
}
return result;
}
int main(){
printf("Hello, Cpp!!\n");
// 過剰数か判定する(数値の約数の合計が数値自体を上回る数)
// 過剰数: 約数の総和が元の数よりも大きくなる数
for(int i=1; i<100; i++){
const int result = tpd(i);
if(i < result){
printf("Result: num: %d < total: %d\n", i, result);
}
}
return 0;
}
//==========
// 友愛数か判定する
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <stdio.h>
int tpd(const int num){
int result = 1;
const int root = sqrt(num);
for(int i=2; i<=root; i++){
if(num%i != 0) continue;
result += (i==(num/i)) ? i:(i+num/i);
}
return result;
}
bool isAmicable(const int num){
const int num1 = tpd(num);
const int num2 = tpd(num1);
//printf("isAmicable: %d, %d, %d\n", num, num1, num2);
return num == num2;
}
int main(){
printf("Hello, Cpp!!\n");
// 友愛数か判定する
// 友愛数: 一方の約数の総和がもう片方の約数の総和と等しい数
for(int i=0; i<300; i++){
if(!isAmicable(i)) continue;
printf("This is amicable num: %d\n", i);
}
return 0;
}
//==========
// アームストロング数
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <stdio.h>
bool isArmstrong(const int num){
const int n1 = num / 100;
const int n2 = (num/10) % 10;
const int n3 = num % 10;
const int total = pow(n1, 3) + pow(n2, 3) + pow(n3, 3);
//printf("%d, %d, %d, %d\n", n1, n2, n3, total);
return num == total;
}
int main(){
printf("Hello, Cpp!!\n");
// アームストロング数か判定する(3桁とする)
// アームストロング数: 桁数nの各桁のn乗の総和が元の数と等しい数
for(int i=100; i<300; i++){
if(!isArmstrong(i)) continue;
printf("This is armstrong num: %d\n", i);
}
return 0;
}
//==========
// 素因数分解
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
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;
}
vector<int> getPrimeFactors(int num){
vector<int> nums = {};
while(num%2 == 0){
nums.push_back(2);
num /= 2;
}
const int root = sqrt(num);
for(int i=3; i<=root; i+=2){
while(num%i == 0 && isPrime(i)){
nums.push_back(i);
num /= i;
}
}
if(2 < num) nums.push_back(num);
return nums;
}
int main(){
printf("Hello, Cpp!!\n");
// 素因数分解
const vector<int> nums = getPrimeFactors(748);
for(auto n:nums){
printf("n: %d\n", n);
}
return 0;
}
//==========
// グレイコード
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
vector<int> d2b(int num){
vector<int> nums;
while(num != 0){
nums.push_back((num%2==0) ? 0:1);
num /= 2;
}
reverse(nums.begin(), nums.end());
return nums;
}
int b2d(const vector<int> nums){
int num = 0;
for(unsigned int i=0; i<nums.size(); i++){
if(nums[i] == 0) continue;
num += pow(2, nums.size()-i-1);
}
return num;
}
string v2s(const vector<int> nums){
stringstream ss;
for(auto num: nums) ss << to_string(num);
return ss.str();
}
int grayEncode(const unsigned int num){
return num ^ (num>>1);
}
int grayDecode(unsigned int num){
for(unsigned int b = 1U<<31; 1<b; b>>=1){
if(num & b) num ^= b >> 1;
}
return num;
}
int main(){
printf("Hello, Cpp!!\n");
// グレイコード
// グレイコード: 連続する2つの数が1bitだけ異なる数
for(unsigned int i=0; i<16; i++){
auto enc = grayEncode(i);
auto encS = v2s(d2b(enc));// グレイコード
auto dec = grayDecode(enc);
auto decS = v2s(d2b(dec));
printf("[%d] enc[%d]:%s, dec[%d]: %s\n", i, enc, encS.c_str(), dec, decS.c_str());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment