Nwlapcug.com


Come utilizzare Excel per trovare i fattori primi

Come utilizzare Excel per trovare i fattori primi


Un numero primo ha solo se stesso e il numero 1 come divisori. Ad esempio, 13 è un numero primo, perché ha solo 1 e 13 come divisori. Ogni numero può essere scritto come prodotto di numeri primi. Ci sono molti strumenti per capire i numeri primi, tra cui alberi di decisione. È possibile aggiungere facilmente una macro di Excel 2007 per sostituire parte del processo di albero decisionale. Una macro è uno strumento che ti permette di Excel personalizzato con i tuoi pulsanti: in questo caso, un pulsante che calcola i fattori. Una volta che hai aggiunto la macro, basta inserire il numero e Excel calcolerà i fattori.

Istruzioni

1

Chiudere tutte le cartelle di lavoro esistenti e avviare un nuovo foglio di lavoro di Excel. Premere "ALT" più "F11" per avviare l'Editor di Visual Basic.

2

Aprire una cartella di lavoro facendo doppio clic su una cartella di lavoro VBAProject dal menu in alto a sinistra dello schermo.

3

Tagliare e incollare il codice riportato di seguito nella cartella di lavoro vuota:

Sub GetFactors()

Dim Count As Integer
Dim NumToFactor As Single 'Integer limits to < 32768
Dim Factor As Single
Dim y As Single
Dim IntCheck As Single

Count = 0
Do
NumToFactor = _
Application.InputBox(Prompt:="Type integer", Type:=1)
'Force entry of integers greater than 0.
IntCheck = NumToFactor - Int(NumToFactor)
If NumToFactor = 0 Then
Exit Sub
'Cancel is 0 -- allow Cancel.
ElseIf NumToFactor < 1 Then
MsgBox "Please enter an integer greater than zero."
ElseIf IntCheck > 0 Then
MsgBox "Please enter an integer -- no decimals."
End If
'Loop until entry of integer greater than 0.
Loop While NumToFactor <= 0 Or IntCheck > 0
For y = 1 To NumToFactor
'Put message in status bar indicating the integer being checked.
Application.StatusBar = "Checking " & y
Factor = NumToFactor Mod y
'Determine if the result of division with Mod is without _
remainder and thus a "factor".
If Factor = 0 Then
'Enter the factor into a column starting with the active cell.
ActiveCell.Offset(Count, 0).Value = y
'Increase the amount to offset for next value.
Count = Count + 1
End If
Next
'Restore Status Bar.
Application.StatusBar = "Ready"

End Sub

Sub GetPrime()

Dim Count As Integer
Dim BegNum As Single 'Integer limits to < 32768
Dim EndNum As Single
Dim Prime As Single
Dim flag As Integer
Dim IntCheck As Single
Count = 0

Do
BegNum = _
Application.InputBox(Prompt:="Type beginning number.", Type:=1)
'Force entry of integers greater than 0.
IntCheck = BegNum - Int(BegNum)
If BegNum = 0 Then
Exit Sub
'Cancel is 0 -- allow Cancel.
ElseIf BegNum < 1 Then
MsgBox "Please enter an integer greater than zero."
ElseIf IntCheck > 0 Then
MsgBox "Please enter an integer -- no decimals."
End If
'Loop until entry of integer greater than 0.
Loop While BegNum <= 0 Or IntCheck > 0

Do
EndNum = _
Application.InputBox(Prompt:="Type ending number.", Type:=1)
'Force entry of integers greater than 0.
IntCheck = EndNum - Int(EndNum)
If EndNum = 0 Then
Exit Sub
'Cancel is 0 -- allow Cancel.
ElseIf EndNum < BegNum Then
MsgBox "Please enter an integer larger than " & BegNum
ElseIf EndNum < 1 Then
MsgBox "Please enter an integer greater than zero."
ElseIf IntCheck > 0 Then
MsgBox "Please enter an integer -- no decimals."
End If
'Loop until entry of integer greater than 0.
Loop While EndNum < BegNum Or EndNum <= 0 Or IntCheck > 0

For y = BegNum To EndNum
flag = 0
z = 1
Do Until flag = 1 Or z = y + 1
'Put message into Status Bar indicating the integer and _
divisor in each loop.
Application.StatusBar = y & " / " & z
Prime = y Mod z
If Prime = 0 And z <> y And z <> 1 Then
flag = 1
End If
z = z + 1
Loop

If flag = 0 Then
'Enter the factor into a column starting with the active cell.
ActiveCell.Offset(Count, 0).Value = y
'Increase the amount to offset for next value.
Count = Count + 1
End If
Next y
'Restore Status Bar.
Application.StatusBar = "Ready"

End Sub

4

Fare clic su "File" e scegliere "Chiudi e torna a Microsoft Excel".

5

Fare clic sul pulsante Microsoft Office in Excel. Fare clic su "Opzioni di Excel", "Personalizza" e quindi selezionare "Macro" nei comandi "Scegli".

6

Nell'elenco, fare clic sulla macro "ThisWorkbook.GetFactors" e quindi fare clic su "Aggiungi". Fare clic su "OK". Questo aggiunge la macro alla barra di accesso rapido nella parte superiore sinistra di Excel.

7

Selezionare la cella in cui si desidera i dati per iniziare.

8

Fare clic sul pulsante macro sulla barra degli strumenti accesso rapido. Immettere il numero che si desidera trovare i fattori primi per. Excel restituirà un elenco di tutti i fattori (tra cui primo e non primo) nella colonna selezionata. Ad esempio, i fattori di 30 sono 1, 2, 3, 5, 6, 10, 15 e 30.

9

Selezionare i numeri primi dall'elenco specificato. 2, 3 e 5 sono i numeri primi nell'elenco. Questi sono i fattori principali per il numero dato.

Consigli & Avvertenze

  • Sia Excel calcolare le divisioni per voi aggiungendo le equazioni relative alle celle che restituiscono i fattori, invece di calcolare a mano.