Das_Bot / Julia.py
omegaT4224's picture
Create Julia.py
eb3d4c8 verified
Raw
History Blame Contribute Delete
5.94 kB
#!/usr/bin/env python3
"""
Standard Mathematical Fractal Generator & Cryptographic Chain Verifier
This script provides two functional, objective programming tools:
1. A standard Julia Set generator that computes complex plane iterations and
saves the resulting fractal visualization as a text-based ASCII art representation.
2. A basic cryptographic ledger simulation demonstrating how blocks of data are
sequentially linked using standard SHA-256 hashing to verify integrity.
"""
import hashlib
import json
import time
# ==============================================================================
# 1. STANDARD CRYPTOGRAPHIC LEDGER SIMULATION
# ==============================================================================
class SimpleBlock:
def __init__(self, index, previous_hash, timestamp, data):
self.index = index
self.previous_hash = previous_hash
self.timestamp = timestamp
self.data = data
self.hash = self.calculate_hash()
def calculate_hash(self):
"""Computes a standard SHA-256 hash of the block contents."""
block_string = json.dumps({
"index": self.index,
"previous_hash": self.previous_hash,
"timestamp": self.timestamp,
"data": self.data
}, sort_keys=True)
return hashlib.sha256(block_string.encode('utf-8')).hexdigest()
class SimpleLedger:
def __init__(self):
self.chain = [self.create_genesis_block()]
def create_genesis_block(self):
"""Initializes the chain with a standard static starting block."""
return SimpleBlock(0, "0", 1718300000, "Genesis Data")
def get_latest_block(self):
return self.chain[-1]
def add_data(self, data):
"""Appends a new block containing arbitrary verification data."""
latest = self.get_latest_block()
new_block = SimpleBlock(
index=latest.index + 1,
previous_hash=latest.hash,
timestamp=int(time.time()),
data=data
)
self.chain.append(new_block)
return new_block
def verify_integrity(self):
"""
Walks the chain block-by-block to confirm that hashes match
and the cryptographic sequence is unbroken.
"""
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i-1]
# Recalculate hash to detect data tampering
if current.hash != current.calculate_hash():
return False, f"Block {i} data has been altered."
# Verify the chain linkage
if current.previous_hash != previous.hash:
return False, f"Block {i} link to Block {i-1} is broken."
return True, "Ledger integrity verified. Sequence is intact."
# ==============================================================================
# 2. JULIA SET FRACTAL GENERATOR (ASCII REPRESENTATION)
# ==============================================================================
def generate_ascii_julia(width=80, height=40, max_iter=30, c_real=-0.7, c_imag=0.27015):
"""
Generates a standard Julia set fractal using the quadratic formula f(z) = z^2 + c.
Outputs a text-based (ASCII) representation of the fractal boundaries.
"""
# Define complex plane boundaries
x_min, x_max = -1.5, 1.5
y_min, y_max = -1.0, 1.0
# ASCII scale representation
chars = " .:-=+*#%@"
output = []
for y_img in range(height):
row = ""
# Map grid index to the imaginary component of z
zy = y_min + (y_img / (height - 1)) * (y_max - y_min)
for x_img in range(width):
# Map grid index to the real component of z
zx = x_min + (x_img / (width - 1)) * (x_max - x_min)
z = complex(zx, zy)
c = complex(c_real, c_imag)
n = 0
# Standard escape velocity check (divergence threshold = 2.0)
while abs(z) <= 2.0 and n < max_iter:
z = z**2 + c
n += 1
# Map iteration count to ASCII shading character
char_index = int((n / max_iter) * (len(chars) - 1))
row += chars[char_index]
output.append(row)
return "\n".join(output)
# ==============================================================================
# 3. EXECUTION CONTROL
# ==============================================================================
if __name__ == "__main__":
print("=" * 80)
print(" OBJECTIVE MATHEMATICS & LOGIC PLATFORM")
print("=" * 80)
# Execute and display the Julia Set Fractal
print("\n[1] Generating Julia Set Fractal (c = -0.7 + 0.27i)...")
print("-" * 80)
fractal_text = generate_ascii_julia()
print(fractal_text)
print("-" * 80)
print("Fractal computed successfully using standard complex iteration: z(n+1) = z(n)^2 + c")
# Execute and verify the cryptographic chain simulation
print("\n[2] Initializing Cryptographic Verification Ledger...")
ledger = SimpleLedger()
# Simulate adding records
ledger.add_data("System initialization verified.")
ledger.add_data("Parameter constraints checked.")
ledger.add_data("Heartbeat event recorded.")
# Audit sequence integrity
is_valid, report = ledger.verify_integrity()
print(f"Total blocks in ledger: {len(ledger.chain)}")
print(f"Audit Status: {'PASS' if is_valid else 'FAIL'}")
print(f"Audit Report: {report}")
# Display sample block data
print("\nSample block entry data (Block #2):")
sample_block = ledger.chain[2]
print(json.dumps({
"Index": sample_block.index,
"Timestamp": sample_block.timestamp,
"Previous Hash": sample_block.previous_hash[:16] + "...",
"Block Hash": sample_block.hash[:16] + "...",
"Payload": sample_block.data
}, indent=4))
print("=" * 80)