2016-09-18 11 views
0

私はユーザに番号を入力するように求めた後、連続した25個の素数を出力する必要があります。以下のコードは実際には素数を印刷しますが、空白を表示しています。素数だけを印刷し、空白を入れないようにする方法を理解できません。私はこれまで、これを持っている:素数を印刷

Public Function IsPrime(value) 
Dim remainder As Double 
Dim rounded As Long 
Dim Max As Long 
Dim j As Long 

IsPrime = 0            
Max = 1 + Int(value/2)       
For j = 2 To Max          
    rounded = Int(value/j)      
    remainder = (value/j) - rounded 

    If remainder = 0 Then 
     Exit For 
    End If 

Next j 

If j = Max + 1 Or value = 2 Then   
    IsPrime = 1 
End If 

If value <= 1 Then 
    IsPrime = 0 
End If 

End Function 



Public Sub printprime() 
Dim x As Integer 
Dim row As Integer 
Dim col As Integer 
Dim j As Integer 
Dim matrix1(1 To 5, 1 To 5) As Integer 


x = InputBox("Please enter a number ") 
j = 1 
For row = 1 To 5 
    For col = 1 To 5 
    If IsPrime(x + j) = 1 Then 
     Cells(row, col) = x + j 
     matrix1(row, col) = Cells(row, col) 
    End If 
    j = j + 1 
    Next col 
Next row 
+0

私はあなただけの1行の数字を埋めることから始めることを示唆しています。素数が見つかった場合にのみ、列番号をインクリメントする必要があります。それがうまくいったら、行と列で塗りつぶすように変更してください。ヒント:mod関数は後者のために役立ちます。 –

+0

ここでは、インクリメントを置いて、プライムが見つかった場合にのみインクリメントを指示しますか? @AndrewMorton – johnbowen

+0

ノーマインド、私はそれを考え出した! @AndrewMorton – johnbowen

答えて

0

あなたはただ続けるだけの素数を記録する必要があります。

Public Sub printprime() 
    Dim x As Long 
    Dim row As Long 
    Dim col As Long 
    Dim j As Long 

    x = InputBox("Please enter a number ") 

    row = 1 
    col = 1 
    For j = x + 1 To 9999 
     If IsPrime(j) Then 
      Cells(row, col) = j 
      col = col + 1 
      If col = 6 Then 
       col = 1 
       row = row + 1 
      End If 
      If row = 7 Then Exit Sub 
     End If 
    Next j 
End Sub 

enter image description here

+0

私は自分の投稿でそうしようとしていたように、私は行列を組み込む必要があります。それを助けてくれる? – johnbowen

+1

9999ではなく20の素数までループするほうがよい。入力数は9000であり、その間隔で20の素数が見つからないことがある。 isPrimeの部分にインクリメントを挿入することができます。 –