Primes

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
I want to generate prime nonprime series 2, 7, 19, 29, 53, 71, 97, 103, 137, 173, 193, 233, ... from the initial primes 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229.

A “prime nonprime” prime number is a prime number that occupies a prime-numbered position in the list of all non-prime numbers.

The PARI code below ...
Rich (BB code):
(PARI) c(n) = {for(k=0, PrimePi(n), IsPrime(n++)&&k--); n};
t(n) = if(n<3, n-1, c(n-2));
vector(50, n, Primes(t(Primes(n))))

... is essentially picking from primes ... 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229

to output 2, , , 7, , , , 19, , 29, , , , , , 53, , , , 71, , , , , 97, , 103, , , , , , 137, , , , , , , 173, , , , 193, , , , , ,

I have coded PrimePi and IsPrime... But I am unable to implement the PARI code in VBA ...

VBA Code:
Function PrimePi(n As Variant) As Integer

    Dim i As Integer, j As Integer
    Dim IsPrime As Boolean
    Dim Count As Integer

    Count = 0&

    For i = 2& To n
        IsPrime = True
        For j = 2& To Sqr(i)
            If i Mod j = 0& Then
                IsPrime = False
                Exit For
            End If
        Next j
        If IsPrime Then Count = Count + 1&
    Next i

    PrimePi = Count

End Function
Function IsPrime(n As Variant) As Boolean

    Dim i As Variant

    If n < 2 Then IsPrime = False: Exit Function

    For i = 2 To Sqr(n)
        If n Mod i = 0 Then IsPrime = False: Exit Function
    Next i

    IsPrime = True

End Function
Rich (BB code):
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I am not familiar with the "&" after numbers like I see in your code. That doesn't mean it is not acceptable, it only means that I do not recall seeing it used in VBA.

Out of curiosity, how are you using the prime nonprime numbers?

I threw this together to see if I understood how to determine the correct numbers...
Book1
ABC
1PrimesNonprimesPrime nonprime
220
3312
4547
57613
611819
713923
8171029
9191237
10231443
11291547
12311653
13371861
14412071
15432173
16472279
17532489
18592597
196126101
206727103
217128107
227330113
237932131
248333137
258934139
269735149
2710136151
2810338163
2910739167
3010940173
3111342181
3212744193
3313145197
3413746199
3513948223
3614949227
3715150229
3815751
3916352
4016754
4117355
4217956
4318157
4419158
4519360
4619762
4719963
4821164
4922365
5022766
5122968
Sheet8
Cell Formulas
RangeFormula
B2:B51B2=LET(seq,SEQUENCE(69,,0,1),FILTER(seq,NOT(ISNUMBER(MATCH(seq,$A$2:$A$51,0)))))
C3:C37C3=INDEX($A$2:$A$51,B3)
Dynamic array formulas.


Doug
 
Upvote 0
Was my interpretation incorrect? Six is a non-prime number and the sixth prime number is 13, but it is not on the list. My thought is that if we can get it working on the worksheet, it should be able to be coded.

Doug
 
Upvote 0
Once you have your primes and non-primes, it's relatively simple to generate A144589:

ABCDEFGHIJ
1N50
2VBA:
3A000027A000040A141468A144570A144589 A000040A141468A144570A144589
4120122012
5231473147
635481954819
74761029761029
851181611816
961392013920
107171025171025
118191227191227
129231433231433
1310291540291540
1411311644311644
151237183718
161341204120
171443214321
181547224722
19162424
20172525
21182626
22192727
23202828
24213030
25223232
26233333
27243434
28253535
29263636
30273838
31283939
32294040
33304242
34314444
35324545
36334646
37344848
38354949
39365050
4037
4138
4239
4340
4441
4542
4643
4744
4845
4946
5047
5148
5249
5350
54
Sheet1
Cell Formulas
RangeFormula
A4:A53A4=SEQUENCE(N)
B4:B18B4=DROP(FILTER(A4#,BYROW(A4#,LAMBDA(r,SUM(--(MOD(r,SEQUENCE(,SQRT(r)))=0))))=1),1)
C4:C39C4=LET(seq,SEQUENCE(N+1,,0),FILTER(seq,NOT(ISNUMBER(MATCH(seq,B4#,)))))
D4:D18D4=IFERROR(INDEX(C4#,B4#),"")
E4:E18E4=IFERROR(INDEX(B4#,D4#),"")
Dynamic array formulas.
Named Ranges
NameRefers ToCells
N=Sheet3!$B$1C4, A4


VBA Code:
Sub Test()

    '1: A000040, 2: A141468, 3: A144570, 4: A144589
    Dim N As Long, Result() As Variant, Count(1 To 2) As Long, i As Long, j As Long
    Dim Prime As Boolean
    
    N = Range("N").Value
    ReDim Result(1 To N + 1, 1 To 4)
    Result(1, 2) = 0
    Result(2, 2) = 1
    Count(2) = 2
    
    On Error Resume Next
    Range("MyResults").ClearContents
    On Error GoTo 0
    
    For i = 2 To N
        Prime = True
        For j = 2 To Sqr(i)
            If i Mod j = 0 Then
                Prime = False
                Count(2) = Count(2) + 1
                Result(Count(2), 2) = i
                Exit For
            End If
        Next j
        If Prime Then
            Count(1) = Count(1) + 1
            Result(Count(1), 1) = i
        End If
    Next i
    
    'Get A144570
    i = 1
    Do Until Result(i, 1) > Count(2)
        Result(i, 3) = Result(Result(i, 1), 2)
        i = i + 1
    Loop
    
    'Get A144589
    i = 1
    Do Until Result(i, 3) > Count(1)
        Result(i, 4) = Result(Result(i, 3), 1)
        i = i + 1
    Loop
    
    With Range("G4:J4").Resize(N + 1)
        .Value = Result
        .Name = "MyResults"
    End With
    
End Sub
 
Last edited:
Upvote 0
The Excel formulae and the VBA code are two different possible solutions. The VBA code involves a worksheet only to:

a) Get the value of N, and
b) Output the results.

Neither of these VBA/Excel interactions is essential.

When you say "VBA solution", what is the question? For example, perhaps you want VBA to generate the first M values of A144589?
 
Upvote 0
@StephenCrump; By VBA solution I mean without using Excel Cells ... essentially an independent VBA function that generates the first n elements.
 
Upvote 0
Ok, I got it ... instead of writing to a sheet ... I created an array to store. Thanks!
 
Upvote 0
Ok, I got it ... instead of writing to a sheet ... I created an array to store. Thanks!
That's great!

You might find this a bit faster:

Rich (BB code):
Sub Test()

    Dim MySequence() As Long, m As Long
   
    m = 1000
    MySequence = GenerateA144589(m)
    Range("A1").Resize(UBound(MySequence)).Value = Application.Transpose(MySequence)

End Sub
Function GenerateA144589(m As Long) As Long()

    '1: A000040, 2: A141468, 3: A144570, 4: A144589
    Dim N As Long, Result() As Long, Count(1 To 2) As Long, i As Long, j As Long, Output() As Long
    Dim Prime As Boolean
   
    N = 100000
    ReDim Result(1 To N + 1, 1 To 3)
    ReDim Output(1 To m)
    Result(1, 2) = 0
    Result(2, 2) = 1
    Count(2) = 2
       
    For i = 2 To N
        Prime = True
        For j = 2 To Sqr(i)
            If i Mod j = 0 Then
                Prime = False
                Count(2) = Count(2) + 1
                Result(Count(2), 2) = i
                Exit For
            End If
        Next j
        If Prime Then
            Count(1) = Count(1) + 1
            Result(Count(1), 1) = i
        End If
    Next i
   
    'Get A144570
    i = 1
    Do Until Result(i, 1) > Count(2)
        Result(i, 3) = Result(Result(i, 1), 2)
        i = i + 1
    Loop
   
    'Get A144589
    For i = 1 To m
        If Result(i, 3) <= Count(1) Then
            Output(i) = Result(Result(i, 3), 1)
        Else
            ReDim Preserve Output(1 To i - 1)
            Exit For
        End If
    Next i
       
    GenerateA144589 = Output
       
End Function

Have a play with the values m and N highlighted in red above. I'm guessing that given the prime number theorem, the distribution of the sequence A144589 can also be predicted. In which case you could make an estimate for N sufficient to get m results.

Instead, I have just experimented with some big values, e.g. m =30,000, N=5,000,000, which on my machine takes only a few seconds to print the first 27,493 values of the sequence.

If you want materially more results than this, you'll run into either memory problems or slow computation, and a smarter approach would be necessary.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top