Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
import numpy as np
|
| 6 |
-
from
|
| 7 |
-
import
|
| 8 |
|
| 9 |
# Historical researchers of perfect numbers - Enhanced HTML version
|
| 10 |
MATHEMATICIANS = """
|
|
@@ -70,13 +70,18 @@ def prime_factorization(n):
|
|
| 70 |
return factors
|
| 71 |
|
| 72 |
def divisors(n):
|
| 73 |
-
"""Find all divisors of n"""
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if n % i == 0:
|
| 77 |
-
divs.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
return sorted(divs)
|
| 81 |
|
| 82 |
def classify_number(n, proper_sum):
|
|
@@ -101,119 +106,140 @@ def check_perfect(n):
|
|
| 101 |
return proper_sum == n, divs, proper_sum, classification, emoji
|
| 102 |
|
| 103 |
def create_enhanced_visualization(n):
|
| 104 |
-
"""Create comprehensive visualization with
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
if n <= 0:
|
| 107 |
return "Please enter a positive integer.", None
|
| 108 |
|
| 109 |
-
# Warning for very large numbers
|
| 110 |
-
if n > 10000000:
|
| 111 |
-
return f"โ ๏ธ Warning: {n} is too large for efficient visualization. Please try a number below 10,000,000.", None
|
| 112 |
-
|
| 113 |
is_perfect, divs, proper_sum, classification, emoji = check_perfect(n)
|
| 114 |
proper_divs = [d for d in divs if d != n]
|
| 115 |
|
| 116 |
-
# Create
|
| 117 |
-
fig =
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Define color scheme
|
| 121 |
if classification == "Perfect":
|
| 122 |
-
color_scheme = plt.cm.Greens
|
| 123 |
main_color = '#2ecc71'
|
|
|
|
| 124 |
elif classification == "Abundant":
|
| 125 |
-
color_scheme = plt.cm.Blues
|
| 126 |
main_color = '#3498db'
|
|
|
|
| 127 |
else:
|
| 128 |
-
color_scheme = plt.cm.Reds
|
| 129 |
main_color = '#e74c3c'
|
|
|
|
| 130 |
|
| 131 |
# 1. PIE CHART - Divisor Distribution
|
| 132 |
-
ax1 = plt.subplot(2, 3, 1)
|
| 133 |
if proper_divs:
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
)
|
| 144 |
-
for autotext in autotexts:
|
| 145 |
-
autotext.set_color('white')
|
| 146 |
-
autotext.set_fontweight('bold')
|
| 147 |
-
ax1.set_title(f'Proper Divisors of {n}', fontsize=12, fontweight='bold', pad=20)
|
| 148 |
|
| 149 |
# 2. BAR CHART - Divisor Values
|
| 150 |
-
ax2 = plt.subplot(2, 3, 2)
|
| 151 |
if proper_divs:
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
| 165 |
|
| 166 |
# 3. COMPARISON BAR - Sum vs Number
|
| 167 |
-
ax3 = plt.subplot(2, 3, 3)
|
| 168 |
comparison = [proper_sum, n]
|
| 169 |
-
labels = [f'Sum of Divisors
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
# Add difference annotation
|
| 176 |
diff = abs(proper_sum - n)
|
| 177 |
-
diff_pct = (diff / n) * 100
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
# 4. DIVISOR PAIRS VISUALIZATION
|
| 185 |
-
|
| 186 |
-
ax4.axis('off')
|
| 187 |
-
|
| 188 |
-
# Show divisor pairs
|
| 189 |
-
pairs_text = "๐ **Divisor Pairs:**\n\n"
|
| 190 |
-
sqrt_n = int(n**0.5)
|
| 191 |
pairs = []
|
| 192 |
for d in proper_divs:
|
| 193 |
if d <= sqrt_n and d != n // d:
|
| 194 |
pairs.append((d, n // d))
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
# 5. PRIME FACTORIZATION
|
| 208 |
-
ax5 = plt.subplot(2, 3, 5)
|
| 209 |
-
ax5.axis('off')
|
| 210 |
-
|
| 211 |
prime_factors = prime_factorization(n)
|
| 212 |
-
factor_text = f"๐ข **Prime Factorization of {n}:**\n\n"
|
| 213 |
-
|
| 214 |
if prime_factors:
|
| 215 |
-
# Count occurrences
|
| 216 |
-
from collections import Counter
|
| 217 |
factor_counts = Counter(prime_factors)
|
| 218 |
factor_parts = []
|
| 219 |
for prime in sorted(factor_counts.keys()):
|
|
@@ -222,50 +248,61 @@ def create_enhanced_visualization(n):
|
|
| 222 |
factor_parts.append(f"{prime}")
|
| 223 |
else:
|
| 224 |
factor_parts.append(f"{prime}^{count}")
|
| 225 |
-
|
| 226 |
else:
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
# 6. MATHEMATICAL FORMULA
|
| 240 |
-
ax6 = plt.subplot(2, 3, 6)
|
| 241 |
-
ax6.axis('off')
|
| 242 |
-
|
| 243 |
-
formula_text = "๐ **Perfect Number Definition:**\n\n"
|
| 244 |
-
formula_text += "A perfect number equals the sum\n"
|
| 245 |
-
formula_text += "of its proper divisors:\n\n"
|
| 246 |
-
|
| 247 |
if proper_divs:
|
| 248 |
-
sum_parts = " + ".join([str(d) for d in proper_divs])
|
| 249 |
-
if len(
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
if is_perfect:
|
| 255 |
-
|
| 256 |
-
|
|
|
|
| 257 |
else:
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
# Generate detailed message with HTML formatting
|
| 271 |
msg = f"""
|
|
@@ -280,14 +317,14 @@ def create_enhanced_visualization(n):
|
|
| 280 |
|
| 281 |
<div style='background: white; padding: 20px; border-radius: 10px; margin-bottom: 20px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border-left: 6px solid #2196F3;'>
|
| 282 |
<h3 style='color: #1976D2; margin-top: 0;'>๐ Divisor Information</h3>
|
| 283 |
-
<p style='font-size: 16px; line-height: 1.8; color: #000;'><strong style='color: #000;'>All Divisors:</strong> <span style='color: #0D47A1; font-weight: bold;'>{divs}</span></p>
|
| 284 |
-
<p style='font-size: 16px; line-height: 1.8; color: #000;'><strong style='color: #000;'>Proper Divisors (excluding {n}):</strong> <span style='color: #0D47A1; font-weight: bold;'>{proper_divs}</span></p>
|
| 285 |
</div>
|
| 286 |
|
| 287 |
<div style='background: white; padding: 20px; border-radius: 10px; margin-bottom: 20px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border-left: 6px solid #FF9800;'>
|
| 288 |
<h3 style='color: #F57C00; margin-top: 0;'>๐งฎ Sum Calculation</h3>
|
| 289 |
<p style='font-size: 18px; line-height: 1.8; font-weight: bold;'>
|
| 290 |
-
{' + '.join([f"<span style='background: #FFE0B2; color: #BF360C; padding: 3px 8px; border-radius: 5px; margin: 2px; display: inline-block;'>{d}</span>" for d in proper_divs])} = <span style='background: #FF6F00; color: white; padding: 5px 12px; border-radius: 5px;'>{proper_sum}</span>
|
| 291 |
</p>
|
| 292 |
</div>
|
| 293 |
|
|
@@ -312,31 +349,32 @@ def create_enhanced_visualization(n):
|
|
| 312 |
# Create Gradio interface
|
| 313 |
with gr.Blocks(theme=gr.themes.Soft(), title="Perfect Number Explorer") as demo:
|
| 314 |
gr.Markdown("""
|
| 315 |
-
# ๐ข Perfect Number Visualizer & Explorer
|
| 316 |
|
| 317 |
### What are Perfect Numbers?
|
| 318 |
A **perfect number** is a positive integer that equals the sum of its proper divisors (divisors excluding the number itself).
|
| 319 |
|
| 320 |
**Example:** 6 is perfect because 1 + 2 + 3 = 6
|
| 321 |
|
| 322 |
-
### Try these perfect numbers: 6, 28, 496, 8128
|
|
|
|
| 323 |
""")
|
| 324 |
|
| 325 |
with gr.Row():
|
| 326 |
with gr.Column(scale=1):
|
| 327 |
number_input = gr.Number(
|
| 328 |
-
label="Enter
|
| 329 |
value=6,
|
| 330 |
precision=0
|
| 331 |
)
|
| 332 |
analyze_btn = gr.Button("๐ Analyze Number", variant="primary", size="lg")
|
| 333 |
|
| 334 |
gr.Markdown("""
|
| 335 |
-
###
|
| 336 |
-
-
|
| 337 |
-
-
|
| 338 |
-
-
|
| 339 |
-
-
|
| 340 |
""")
|
| 341 |
|
| 342 |
with gr.Row():
|
|
@@ -345,7 +383,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Perfect Number Explorer") as demo:
|
|
| 345 |
)
|
| 346 |
|
| 347 |
with gr.Row():
|
| 348 |
-
plot_output = gr.Plot(label="๐ Visual Analysis")
|
| 349 |
|
| 350 |
analyze_btn.click(
|
| 351 |
fn=create_enhanced_visualization,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
from plotly.subplots import make_subplots
|
| 5 |
import numpy as np
|
| 6 |
+
from collections import Counter
|
| 7 |
+
import math
|
| 8 |
|
| 9 |
# Historical researchers of perfect numbers - Enhanced HTML version
|
| 10 |
MATHEMATICIANS = """
|
|
|
|
| 70 |
return factors
|
| 71 |
|
| 72 |
def divisors(n):
|
| 73 |
+
"""Find all divisors of n efficiently"""
|
| 74 |
+
if n == 1:
|
| 75 |
+
return [1]
|
| 76 |
+
|
| 77 |
+
divs = set()
|
| 78 |
+
sqrt_n = int(math.isqrt(n))
|
| 79 |
+
|
| 80 |
+
for i in range(1, sqrt_n + 1):
|
| 81 |
if n % i == 0:
|
| 82 |
+
divs.add(i)
|
| 83 |
+
divs.add(n // i)
|
| 84 |
+
|
| 85 |
return sorted(divs)
|
| 86 |
|
| 87 |
def classify_number(n, proper_sum):
|
|
|
|
| 106 |
return proper_sum == n, divs, proper_sum, classification, emoji
|
| 107 |
|
| 108 |
def create_enhanced_visualization(n):
|
| 109 |
+
"""Create comprehensive visualization with Plotly for any positive integer"""
|
| 110 |
+
try:
|
| 111 |
+
n = int(n)
|
| 112 |
+
except (ValueError, TypeError):
|
| 113 |
+
return "Please enter a valid positive integer.", None
|
| 114 |
+
|
| 115 |
if n <= 0:
|
| 116 |
return "Please enter a positive integer.", None
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
is_perfect, divs, proper_sum, classification, emoji = check_perfect(n)
|
| 119 |
proper_divs = [d for d in divs if d != n]
|
| 120 |
|
| 121 |
+
# Create subplots with Plotly
|
| 122 |
+
fig = make_subplots(
|
| 123 |
+
rows=2, cols=3,
|
| 124 |
+
subplot_titles=(
|
| 125 |
+
f'Proper Divisors of {n}',
|
| 126 |
+
'Divisor Magnitudes',
|
| 127 |
+
f'{classification} Number Analysis {emoji}',
|
| 128 |
+
'Divisor Pairs',
|
| 129 |
+
f'Prime Factorization of {n}',
|
| 130 |
+
'Perfect Number Definition'
|
| 131 |
+
),
|
| 132 |
+
specs=[
|
| 133 |
+
[{"type": "pie"}, {"type": "bar"}, {"type": "bar"}],
|
| 134 |
+
[{"type": "table"}, {"type": "table"}, {"type": "table"}]
|
| 135 |
+
],
|
| 136 |
+
vertical_spacing=0.1,
|
| 137 |
+
horizontal_spacing=0.05
|
| 138 |
+
)
|
| 139 |
|
| 140 |
# Define color scheme
|
| 141 |
if classification == "Perfect":
|
|
|
|
| 142 |
main_color = '#2ecc71'
|
| 143 |
+
colors = px.colors.sequential.Greens[2:8]
|
| 144 |
elif classification == "Abundant":
|
|
|
|
| 145 |
main_color = '#3498db'
|
| 146 |
+
colors = px.colors.sequential.Blues[2:8]
|
| 147 |
else:
|
|
|
|
| 148 |
main_color = '#e74c3c'
|
| 149 |
+
colors = px.colors.sequential.Reds[2:8]
|
| 150 |
|
| 151 |
# 1. PIE CHART - Divisor Distribution
|
|
|
|
| 152 |
if proper_divs:
|
| 153 |
+
# Limit to top 20 divisors for readability
|
| 154 |
+
display_divs = proper_divs[:20] if len(proper_divs) > 20 else proper_divs
|
| 155 |
+
fig.add_trace(
|
| 156 |
+
go.Pie(
|
| 157 |
+
labels=[str(d) for d in display_divs],
|
| 158 |
+
values=display_divs,
|
| 159 |
+
textinfo='label+percent',
|
| 160 |
+
insidetextorientation='radial',
|
| 161 |
+
marker=dict(colors=colors * (len(display_divs) // len(colors) + 1)),
|
| 162 |
+
textfont=dict(size=12),
|
| 163 |
+
hovertemplate="Divisor: %{label}<br>Value: %{value}<extra></extra>"
|
| 164 |
+
),
|
| 165 |
+
row=1, col=1
|
| 166 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
# 2. BAR CHART - Divisor Values
|
|
|
|
| 169 |
if proper_divs:
|
| 170 |
+
# Limit to top 20 divisors for readability
|
| 171 |
+
display_divs = proper_divs[:20] if len(proper_divs) > 20 else proper_divs
|
| 172 |
+
fig.add_trace(
|
| 173 |
+
go.Bar(
|
| 174 |
+
x=[str(d) for d in display_divs],
|
| 175 |
+
y=display_divs,
|
| 176 |
+
marker_color=main_color,
|
| 177 |
+
text=display_divs,
|
| 178 |
+
textposition='outside',
|
| 179 |
+
hovertemplate="Divisor: %{x}<br>Value: %{y}<extra></extra>"
|
| 180 |
+
),
|
| 181 |
+
row=1, col=2
|
| 182 |
+
)
|
| 183 |
+
fig.update_xaxes(tickangle=45, row=1, col=2)
|
| 184 |
|
| 185 |
# 3. COMPARISON BAR - Sum vs Number
|
|
|
|
| 186 |
comparison = [proper_sum, n]
|
| 187 |
+
labels = [f'Sum of Divisors<br>({proper_sum})', f'Number<br>({n})']
|
| 188 |
+
fig.add_trace(
|
| 189 |
+
go.Bar(
|
| 190 |
+
x=labels,
|
| 191 |
+
y=comparison,
|
| 192 |
+
marker_color=[main_color, '#95a5a6'],
|
| 193 |
+
text=comparison,
|
| 194 |
+
textposition='outside',
|
| 195 |
+
hovertemplate="%{x}<br>Value: %{y}<extra></extra>"
|
| 196 |
+
),
|
| 197 |
+
row=1, col=3
|
| 198 |
+
)
|
| 199 |
|
| 200 |
# Add difference annotation
|
| 201 |
diff = abs(proper_sum - n)
|
| 202 |
+
diff_pct = (diff / n) * 100 if n != 0 else 0
|
| 203 |
+
fig.add_annotation(
|
| 204 |
+
x=0.5, y=max(comparison) * 0.5,
|
| 205 |
+
text=f"Difference: {diff}<br>({diff_pct:.1f}%)",
|
| 206 |
+
showarrow=False,
|
| 207 |
+
bgcolor="wheat",
|
| 208 |
+
bordercolor="black",
|
| 209 |
+
font=dict(size=12, color="black"),
|
| 210 |
+
row=1, col=3
|
| 211 |
+
)
|
| 212 |
|
| 213 |
# 4. DIVISOR PAIRS VISUALIZATION
|
| 214 |
+
sqrt_n = int(math.isqrt(n))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
pairs = []
|
| 216 |
for d in proper_divs:
|
| 217 |
if d <= sqrt_n and d != n // d:
|
| 218 |
pairs.append((d, n // d))
|
| 219 |
|
| 220 |
+
if pairs:
|
| 221 |
+
pair_labels = [f"{d1} ร {d2}" for d1, d2 in pairs]
|
| 222 |
+
pair_values = [n] * len(pairs)
|
| 223 |
+
fig.add_trace(
|
| 224 |
+
go.Table(
|
| 225 |
+
header=dict(values=["Divisor Pairs", "Product"]),
|
| 226 |
+
cells=dict(values=[pair_labels, pair_values]),
|
| 227 |
+
columnwidth=[100, 50]
|
| 228 |
+
),
|
| 229 |
+
row=2, col=1
|
| 230 |
+
)
|
| 231 |
+
else:
|
| 232 |
+
fig.add_trace(
|
| 233 |
+
go.Table(
|
| 234 |
+
header=dict(values=["Divisor Pairs"]),
|
| 235 |
+
cells=dict(values=[["No pairs found"]])
|
| 236 |
+
),
|
| 237 |
+
row=2, col=1
|
| 238 |
+
)
|
| 239 |
|
| 240 |
# 5. PRIME FACTORIZATION
|
|
|
|
|
|
|
|
|
|
| 241 |
prime_factors = prime_factorization(n)
|
|
|
|
|
|
|
| 242 |
if prime_factors:
|
|
|
|
|
|
|
| 243 |
factor_counts = Counter(prime_factors)
|
| 244 |
factor_parts = []
|
| 245 |
for prime in sorted(factor_counts.keys()):
|
|
|
|
| 248 |
factor_parts.append(f"{prime}")
|
| 249 |
else:
|
| 250 |
factor_parts.append(f"{prime}^{count}")
|
| 251 |
+
factorization = " ร ".join(factor_parts)
|
| 252 |
else:
|
| 253 |
+
factorization = "1 (unity)"
|
| 254 |
+
|
| 255 |
+
properties = [
|
| 256 |
+
f"Total divisors: {len(divs)}",
|
| 257 |
+
f"Proper divisors: {len(proper_divs)}",
|
| 258 |
+
f"Sum of all divisors: {sum(divs)}",
|
| 259 |
+
f"Classification: {classification} {emoji}"
|
| 260 |
+
]
|
| 261 |
+
|
| 262 |
+
fig.add_trace(
|
| 263 |
+
go.Table(
|
| 264 |
+
header=dict(values=["Prime Factorization", "Number Properties"]),
|
| 265 |
+
cells=dict(values=[[factorization], ["<br>".join(properties)]])
|
| 266 |
+
),
|
| 267 |
+
row=2, col=2
|
| 268 |
+
)
|
| 269 |
|
| 270 |
# 6. MATHEMATICAL FORMULA
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
if proper_divs:
|
| 272 |
+
sum_parts = " + ".join([str(d) for d in proper_divs[:10]]) # Limit to first 10
|
| 273 |
+
if len(proper_divs) > 10:
|
| 274 |
+
sum_parts += " + ..."
|
| 275 |
+
formula = f"{sum_parts} = {proper_sum}"
|
| 276 |
+
else:
|
| 277 |
+
formula = "No proper divisors"
|
| 278 |
+
|
| 279 |
+
result = f"{'โ
' if is_perfect else 'โ'} {proper_sum} {'=' if is_perfect else 'โ '} {n}"
|
| 280 |
|
| 281 |
if is_perfect:
|
| 282 |
+
conclusion = "PERFECT NUMBER! ๐"
|
| 283 |
+
elif classification == "Abundant":
|
| 284 |
+
conclusion = f"Abundant (Excess: {proper_sum - n})"
|
| 285 |
else:
|
| 286 |
+
conclusion = f"Deficient (Deficiency: {n - proper_sum})"
|
| 287 |
+
|
| 288 |
+
fig.add_trace(
|
| 289 |
+
go.Table(
|
| 290 |
+
header=dict(values=["Perfect Number Definition"]),
|
| 291 |
+
cells=dict(values=[[formula, result, conclusion]])
|
| 292 |
+
),
|
| 293 |
+
row=2, col=3
|
| 294 |
+
)
|
| 295 |
|
| 296 |
+
# Update layout
|
| 297 |
+
fig.update_layout(
|
| 298 |
+
height=800,
|
| 299 |
+
showlegend=False,
|
| 300 |
+
title_text=f"Comprehensive Analysis of {n}",
|
| 301 |
+
title_x=0.5,
|
| 302 |
+
title_font_size=24,
|
| 303 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 304 |
+
paper_bgcolor='rgba(0,0,0,0)'
|
| 305 |
+
)
|
| 306 |
|
| 307 |
# Generate detailed message with HTML formatting
|
| 308 |
msg = f"""
|
|
|
|
| 317 |
|
| 318 |
<div style='background: white; padding: 20px; border-radius: 10px; margin-bottom: 20px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border-left: 6px solid #2196F3;'>
|
| 319 |
<h3 style='color: #1976D2; margin-top: 0;'>๐ Divisor Information</h3>
|
| 320 |
+
<p style='font-size: 16px; line-height: 1.8; color: #000;'><strong style='color: #000;'>All Divisors:</strong> <span style='color: #0D47A1; font-weight: bold;'>{divs[:20]}{"..." if len(divs) > 20 else ""}</span></p>
|
| 321 |
+
<p style='font-size: 16px; line-height: 1.8; color: #000;'><strong style='color: #000;'>Proper Divisors (excluding {n}):</strong> <span style='color: #0D47A1; font-weight: bold;'>{proper_divs[:20]}{"..." if len(proper_divs) > 20 else ""}</span></p>
|
| 322 |
</div>
|
| 323 |
|
| 324 |
<div style='background: white; padding: 20px; border-radius: 10px; margin-bottom: 20px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); border-left: 6px solid #FF9800;'>
|
| 325 |
<h3 style='color: #F57C00; margin-top: 0;'>๐งฎ Sum Calculation</h3>
|
| 326 |
<p style='font-size: 18px; line-height: 1.8; font-weight: bold;'>
|
| 327 |
+
{' + '.join([f"<span style='background: #FFE0B2; color: #BF360C; padding: 3px 8px; border-radius: 5px; margin: 2px; display: inline-block;'>{d}</span>" for d in proper_divs[:10]])}{" + ..." if len(proper_divs) > 10 else ""} = <span style='background: #FF6F00; color: white; padding: 5px 12px; border-radius: 5px;'>{proper_sum}</span>
|
| 328 |
</p>
|
| 329 |
</div>
|
| 330 |
|
|
|
|
| 349 |
# Create Gradio interface
|
| 350 |
with gr.Blocks(theme=gr.themes.Soft(), title="Perfect Number Explorer") as demo:
|
| 351 |
gr.Markdown("""
|
| 352 |
+
# ๐ข Perfect Number Visualizer & Explorer (Unlimited Range!)
|
| 353 |
|
| 354 |
### What are Perfect Numbers?
|
| 355 |
A **perfect number** is a positive integer that equals the sum of its proper divisors (divisors excluding the number itself).
|
| 356 |
|
| 357 |
**Example:** 6 is perfect because 1 + 2 + 3 = 6
|
| 358 |
|
| 359 |
+
### Try these perfect numbers: 6, 28, 496, 8128, 33550336
|
| 360 |
+
### Or try very large numbers like 2^31-1 (2147483647) or even larger!
|
| 361 |
""")
|
| 362 |
|
| 363 |
with gr.Row():
|
| 364 |
with gr.Column(scale=1):
|
| 365 |
number_input = gr.Number(
|
| 366 |
+
label="Enter ANY Positive Integer",
|
| 367 |
value=6,
|
| 368 |
precision=0
|
| 369 |
)
|
| 370 |
analyze_btn = gr.Button("๐ Analyze Number", variant="primary", size="lg")
|
| 371 |
|
| 372 |
gr.Markdown("""
|
| 373 |
+
### Key Features:
|
| 374 |
+
- **Unlimited Range**: Analyze ANY positive integer (no upper limit!)
|
| 375 |
+
- **Optimized Algorithms**: Efficiently handles very large numbers
|
| 376 |
+
- **Interactive Visualizations**: Zoom, pan, and hover for details
|
| 377 |
+
- **Comprehensive Analysis**: Divisors, factorization, and classification
|
| 378 |
""")
|
| 379 |
|
| 380 |
with gr.Row():
|
|
|
|
| 383 |
)
|
| 384 |
|
| 385 |
with gr.Row():
|
| 386 |
+
plot_output = gr.Plot(label="๐ Interactive Visual Analysis")
|
| 387 |
|
| 388 |
analyze_btn.click(
|
| 389 |
fn=create_enhanced_visualization,
|