|
|
import gradio as gr |
|
|
import sympy |
|
|
from graphviz import Digraph |
|
|
|
|
|
|
|
|
def asal_carpanlar(sayi): |
|
|
return sympy.factorint(sayi) |
|
|
|
|
|
|
|
|
def carpan_agaci(sayi): |
|
|
factors = sympy.factorint(sayi) |
|
|
if len(factors) == 1 and list(factors.values())[0] == 1: |
|
|
return str(sayi) |
|
|
else: |
|
|
factor = list(factors.keys())[0] |
|
|
return [sayi, carpan_agaci(factor), carpan_agaci(sayi // factor)] |
|
|
|
|
|
|
|
|
def ciz_agac(agac, graph=None): |
|
|
if graph is None: |
|
|
graph = Digraph() |
|
|
if isinstance(agac, list): |
|
|
parent = str(agac[0]) |
|
|
left, right = agac[1], agac[2] |
|
|
for child in [left, right]: |
|
|
child_label = str(child[0]) if isinstance(child, list) else str(child) |
|
|
graph.edge(parent, child_label) |
|
|
if isinstance(child, list): |
|
|
ciz_agac(child, graph) |
|
|
return graph |
|
|
|
|
|
|
|
|
def asal_carpan_gorseli(sayi): |
|
|
agac = carpan_agaci(int(sayi)) |
|
|
graph = ciz_agac(agac) |
|
|
graph.render("asal_carpan_agaci", format="png") |
|
|
return "asal_carpan_agaci.png" |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("### 🔢 Asal Çarpan Ağacı Hesaplama") |
|
|
|
|
|
with gr.Row(): |
|
|
sayi = gr.Number(label="Bir sayı girin", minimum=2, value=48) |
|
|
|
|
|
btn = gr.Button("Asal Çarpan Ağacını Oluştur 🌳") |
|
|
|
|
|
output_img = gr.Image(label="Asal Çarpan Ağacı Görseli", type="filepath") |
|
|
|
|
|
btn.click(asal_carpan_gorseli, inputs=sayi, outputs=output_img) |
|
|
|
|
|
demo.launch() |
|
|
|