0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

計算がはやくなる

Last updated at Posted at 2025-06-09

# Program Name: frequent_math_patterns_1to20.py
# Creation Date: 20250609
# Overview: Computes frequent squares, powers, decimal-fraction conversions, and fast multiplication patterns for numbers 1–20
# Usage: Run the script for outputs of fast math patterns from 1 to 20

# ライブラリのインストール / Install required libraries
!pip install sympy

# ライブラリのインポート / Import libraries
from sympy import Rational, factorint

# ============================================
# 頻出2乗計算(1〜20)/ Frequent Squares (1–20)
# ============================================
# 各整数の2乗を辞書形式で保存 / Store squares of numbers 1–20
squares = {n: n**2 for n in range(1, 21)}
print("■ Frequent Squares (1–20):")
print(squares)

# =============================================
# 頻出累乗計算(1〜5の累乗)/ Frequent Powers
# =============================================
# n^k を小さめの組み合わせで収集 / Collect small base-power combos
powers = {}
for base in range(1, 6):
    for exp in range(2, 5):
        powers[f"{base}^{exp}"] = base**exp
print("\n■ Frequent Powers (base 1–5, exp 2–4):")
print(powers)

# ==========================================================
# 小数⇔分数変換:0.05〜1.0までの0.05刻み / Decimal-Fraction
# ==========================================================
# 0.05 刻みで小数から分数へ変換 / Convert 0.05 steps to fractions
decimals = [round(0.05 * i, 2) for i in range(1, 21)]
fractions = {d: Rational(d).limit_denominator() for d in decimals}
print("\n■ Decimal to Fraction (0.05 – 1.0):")
print(fractions)

# ======================================================
# 偶数×5の倍数の速算 / Even × Multiple of 5 (≤ 20)
# ======================================================
even_numbers = [n for n in range(2, 21, 2)]  # 2, 4, ..., 20
multiple_of_5 = [n for n in range(5, 21, 5)]  # 5, 10, 15, 20
even_5_products = {(a, b): a * b for a in even_numbers for b in multiple_of_5}
print("\n■ Fast Multiplication: Even × Multiple of 5:")
print(even_5_products)

# =====================================================
# (4の倍数)×25 の速算(≤20)/ Multiple of 4 × 25
# =====================================================
multiple_of_4 = [n for n in range(4, 21, 4)]  # 4, 8, 12, 16, 20
result_25 = {f"{x}×25": x * 25 for x in multiple_of_4}
print("\n■ Fast Multiplication: Multiple of 4 × 25:")
print(result_25)

# =====================================================
# (8の倍数)×125 の速算(≤20)/ Multiple of 8 × 125
# =====================================================
multiple_of_8 = [n for n in range(8, 21, 8)]  # 8, 16
result_125 = {f"{x}×125": x * 125 for x in multiple_of_8}
print("\n■ Fast Multiplication: Multiple of 8 × 125:")
print(result_125)

# ================================================
# 恒等式 A²-B² = (A+B)(A-B) の確認
# Identity: A² - B² = (A + B)(A - B)
# ================================================
A = 25
B = 5
lhs = A**2 - B**2
rhs = (A + B) * (A - B)
print("\n■ Identity A² - B² = (A+B)(A−B):")
print(f"A={A}, B={B}, A²−B²={lhs}, (A+B)(A−B)={rhs}")

# ======================================================
# A² = (A+B)(A−B) + B² の展開公式の変形利用 / Identity
# ======================================================
transformed = (A + B)*(A - B) + B**2
print("\n■ Transformed Identity A² = (A+B)(A−B) + B²:")
print(f"(A+B)(A−B) + B² = {transformed}, A² = {A**2}")

# ====================================
# 公約数:a = 171, b = 133 の差を素因数分解
# Prime factorization of a - b
# ====================================
a = 171
b = 133
difference = a - b
factorization = factorint(difference)
print("\n■ a - b =", difference)
print("■ Prime Factorization of a - b:")
print(factorization)

# Program Name: sakuranbozan.py
# Creation Date: 20250609
# Overview: General implementation of "Sakuranbo-zan" for both addition and subtraction, emphasizing place value strategies
# Usage: Use sakuranbo_add(a, b) or sakuranbo_sub(a, b)

# -------------------------------
# さくらんぼ算(加法)/ Sakuranbo-style Addition
# -------------------------------
def sakuranbo_add(a, b):
    print(f"\n■ さくらんぼ算(加法): {a} + {b}")
    
    to_10 = 10 - (a % 10)  # 10にするためにbから取る数 / portion to complete 10 from a
    if to_10 > b:
        # 繰り上がりなし / no carrying
        result = a + b
        print(f"{a} + {b} = {result}(繰り上がりなし / no carrying)")
    else:
        left = to_10
        right = b - to_10
        step1 = a + left
        step2 = step1 + right
        print(f"{b}{left}{right} に分ける(さくらんぼ分解 / cherry split)")
        print(f"{a} + {left} = {step1}(10のかたまり / made 10)")
        print(f"{step1} + {right} = {step2}")
        print(f"✅ 結果 / result: {a} + {b} = {step2}")

# -------------------------------
# さくらんぼ算(減法)/ Sakuranbo-style Subtraction
# -------------------------------
def sakuranbo_sub(a, b):
    print(f"\n■ さくらんぼ算(減法): {a} - {b}")

    if a < b:
        print(f"{a} < {b}:負の数になります / result will be negative")
        print(f"{a} - {b} = {a - b}")
        return

    ones_a = a % 10
    if ones_a >= b % 10 or b < 10:
        # 一の位で引ける / no borrowing
        result = a - b
        print(f"{a} - {b} = {result}(繰り下がりなし / no borrowing)")
    else:
        # 繰り下がり必要 / borrowing needed
        print(f"{a} の一の位:{ones_a}{b} の一の位:{b%10}")
        need = b % 10 - ones_a
        print(f"→ 一の位が足りないので十の位から {need} 借りる / need to borrow {need}")
        new_ones = 10 + ones_a - (b % 10)
        new_tens = (a // 10 - 1) * 10 - (b // 10) * 10
        result = new_tens + new_ones
        print(f"→ 新しい一の位:{new_ones},新しい十の位:{new_tens}")
        print(f"✅ 結果 / result: {a} - {b} = {a - b}")

# -------------------------------
# 実行例 / Examples
# -------------------------------
sakuranbo_add(8, 7)     # 繰り上がりあり
sakuranbo_add(6, 3)     # 繰り上がりなし
sakuranbo_add(9, 1)     # ちょうど10

sakuranbo_sub(13, 8)    # 繰り下がりあり
sakuranbo_sub(15, 4)    # 繰り下がりなし
sakuranbo_sub(57, 19)   # 複雑な繰り下がり
sakuranbo_sub(9, 13)    # 負の数

# 📘 Google Colab 用:二次関数のプロットと式の素因数分解
# F(x) = Ax^2 + Bx + C 形式で描画と計算を行う

# 必要ライブラリのインポート
import numpy as np
import matplotlib.pyplot as plt
from sympy import factorint, sqrt, simplify

# --- 定数定義(A, B, C) ---
A = 1
B = 6
C = 8

# --- 関数定義 ---
def F(x):
    return A * x**2 + B * x + C

# --- プロット(F(x)) ---
x_vals = np.linspace(-10, 2, 400)
y_vals = F(x_vals)

plt.figure(figsize=(8, 5))
plt.plot(x_vals, y_vals, label=f"F(x) = {A}x² + {B}x + {C}")
plt.axhline(0, color='gray', linewidth=0.5)
plt.axvline(0, color='gray', linewidth=0.5)
plt.title("Quadratic Function Plot")
plt.xlabel("x")
plt.ylabel("F(x)")
plt.grid(True)
plt.legend()
plt.show()

# --- 式の計算と素因数分解 ---
exprs = {
    "A": A,
    "B": B,
    "C": C,
    "B^2 - 4AC": B**2 - 4*A*C,
    "2A": 2*A,
    "B / (2A)": B / (2*A),
    "sqrt(B^2 - 4AC)": sqrt(B**2 - 4*A*C),
    "sqrt(B^2 - 4AC) / (2A)": sqrt(B**2 - 4*A*C) / (2*A)
}

print("🔎 素因数分解または簡約表現:")
for label, value in exprs.items():
    if isinstance(value, (int, float)) and float(value).is_integer():
        factors = factorint(int(value))
        print(f"{label}: {int(value)} -> {factors}")
    else:
        simplified = simplify(value)
        print(f"{label}: {simplified}")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?