Dim x, a, b, k As String Private Sub Command1_Click() a = Val(Text1.Text)x = 2 If a <= 1 Or a > Int(a) Then If a = 1 Then Text2.Text = "它既不是质数,也不是合数" Else MsgBox "请您先输入数据", vbOKOnly + vbInformation, "友情提示" End If
Else Do While a / 2 = Int(a / 2) And a >= 4
If b = 0 Then Text2.Text = Text2.Text & "2" b = 1 Else Text2.Text = Text2.Text & "*2" End If a = a / 2 k = a Loop Do While a > 1 For x = 3 To Sqr(a) Step 2 Do While a / x = Int(a / x) And a >= x * x
If b = 0 Then Text2.Text = Text2.Text & x b = 1 Else Text2.Text = Text2.Text & "*" & x End If a = a / x Loop Next k = a a = 1 Loop
If b = 1 Then Text2.Text = Text2.Text & "*" & k Else Text2.Text = "这是一个质数" End If End If End SubPrivate Sub Command2_Click() Text1.Text = "" Text2.Text = "" End Sub
using System;class Program { static void Main() { int n = int.Parse(Console.ReadLine()); int f = Fac(n); Console.Write("{0} = {1}", n, f); while (f != n) { n /= f; f = Fac(n); Console.Write(" * {0}", f); } Console.WriteLine(); }
static int Fac(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) return i; } return n; } }
Private Sub Command1_Click()
a = Val(Text1.Text)x = 2
If a <= 1 Or a > Int(a) Then
If a = 1 Then
Text2.Text = "它既不是质数,也不是合数"
Else
MsgBox "请您先输入数据", vbOKOnly + vbInformation, "友情提示"
End If
Else Do While a / 2 = Int(a / 2) And a >= 4
If b = 0 Then
Text2.Text = Text2.Text & "2"
b = 1
Else
Text2.Text = Text2.Text & "*2"
End If
a = a / 2
k = a
Loop
Do While a > 1
For x = 3 To Sqr(a) Step 2
Do While a / x = Int(a / x) And a >= x * x
If b = 0 Then
Text2.Text = Text2.Text & x
b = 1
Else
Text2.Text = Text2.Text & "*" & x
End If
a = a / x
Loop
Next
k = a
a = 1
Loop
If b = 1 Then
Text2.Text = Text2.Text & "*" & k
Else
Text2.Text = "这是一个质数"
End If
End If
End SubPrivate Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
int f = Fac(n);
Console.Write("{0} = {1}", n, f);
while (f != n)
{
n /= f;
f = Fac(n);
Console.Write(" * {0}", f);
}
Console.WriteLine();
}
static int Fac(int n)
{
for (int i = 2; i < n; i++)
{
if (n % i == 0) return i;
}
return n;
}
}