A few simple Questions

Hey Experts.

I need a few basic questions answered, so i may as well just put them all together.

1) Is there an easy way to set the content of variables p0 - p100 to ""  rather than a long list?

2)  i am currently using the following :

Do
    Do While N Mod X(0) = 0
        N = N / X(0)
            p0 = X(0)
    Exit Do
Loop
Loop Until N Mod X(0) <> 0

Do
    Do While N Mod X(1) = 0
        N = N / X(1)
            p1 = X(1)
        Exit Do
Loop
Loop Until N Mod X(1) <> 0


Is there a loop that will do this for p0 to p100 rather than a long list?


3 and last) Is there a function to put all primes into an array of X (at the minute i have     X = Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41)   but this could go up to 10's of thousands.... and i havnt got that much time)  the X(0),  X(1) in question 2 refers to this, so they need to be compatible as such.


Please post if you want any more info, lots of points going here on simple questions!
Cheers
Cof
LVL 2
cofneverlivetotellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Erick37Commented:
If you declare p as:

Dim p(0 to 100) as string

The the Erase statement will reinit all members to ""

e.g.

Erase p
0
cofneverlivetotellAuthor Commented:
Ok thanks, any advance on other questions people?
0
cofneverlivetotellAuthor Commented:
Uhm - this question doesnt seem to be showing up in the open questions....  What should i do
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

David LeeCommented:
Concerning the prime numbers question, have a look at this: http://www.vb-helper.com/howto_list_primes.html
0
mlmccCommented:
>>Uhm - this question doesnt seem to be showing up in the open questions....  What should i do
Are you looking in the list of your questions?
Are you in the correct forum?
   http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic

mlmcc
0
Stephen MandersonSoftware EngineerCommented:
question 1, you could try

for i = 0 to 100
   Do
    Do While N Mod X(i) = 0
        N = N / X(i)
            p1 = X(i)
        Exit Do
Loop
Loop Until N Mod X(i) <> 0
next
0
Stephen MandersonSoftware EngineerCommented:
make that question 2 sorry
0
Stephen MandersonSoftware EngineerCommented:
would have to declare i as
Dim i as string
0
cofneverlivetotellAuthor Commented:
mlmcc I asked in CS about this, apparently your own questions dont show up in "questions awaiting answers"... just a little confusion

Mr Manderson, i will work that into my program in a few hours (not at home right now) but it looks feasable

Question 1 and 3 are successfully answered, so please nobody post about them :)

Cheers
Cof
0
mlmccCommented:
Questiion 2 - Nothing I am aware of.  Is there a reason you used P0 - P100 rather than an array?

Dim P(100) as integer
 Now you can do what you want quite easily.  I realize the code change may be larger than writing the list.

Another thought would be to use a subroutine for the code and call it with
Call MySub(P0)
Call MySub(P1)
etc

mlmcc
0
Stephen MandersonSoftware EngineerCommented:
Dim i As String

For i = 0 to 100
   Do
    Do While N Mod X(i) = 0
        N = N / X(i)
            p1 = X(i)
        Exit Do
Loop Until N Mod X(i) <> 0
Loop
Next

That should work ok
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cofneverlivetotellAuthor Commented:
Dim i as String gives a type mismatch with "For i = 0 to 100"

and "Loop Until" says there is no Do.

Cof
0
Stephen MandersonSoftware EngineerCommented:
What is it you are trying to do with the code, can you show me. I dont know what x is for, only error i get for it is that x is not defined.

can try this

Dim i As Long

For i = 0 To 100
   Do
    Do While N Mod x(i) = 0
        N = N / x(i)
            p1 = x(i)
        Exit Do
        Loop
Loop Until N Mod x(i) <> 0

Next
0
cofneverlivetotellAuthor Commented:
Ok, here is my complete code:

' Currently only works for up to 4 prime factors

Option Explicit
Dim pctr As Long
Dim max_number As Integer
Dim N As Integer
Dim X(1000) As Variant
Dim Z As Integer
Dim p(1 To 1000) As String
Dim aindex As Integer

Private Sub PopulateArray()
aindex = 0
For pctr = 1 To max_number
If IsPrime(pctr) Then
    X(aindex) = pctr
    aindex = aindex + 1
End If
Next pctr

End Sub

Function IsPrime(lngNumber As Long) As Integer
    Dim lngFactor As Long
    IsPrime = True
    lngFactor = Int(Sqr(lngNumber))
    If lngNumber > 3 Then
          If lngNumber Mod 2 = 0 Then
            IsPrime = False
          Else
            For lngFactor = 3 To lngFactor Step 2
              If lngNumber Mod lngFactor = 0 Then
                IsPrime = False
                Exit For
              End If
            Next
          End If
    End If
End Function

Private Sub Command1_Click()
    Erase p
    max_number = Text1.Text
    Z = Text1.Text
    N = Z
PopulateArray

Dim i As Long

For i = 1 To 100
   Do
    Do While N Mod X(i) = 0
        N = N / X(i)
            p(i) = X(i)
        Exit Do
        Loop
Loop Until N Mod X(i) <> 0

Next

    If p(1) <> "" Then
        Text2.Text = Z * (1 - (1 / p(1)))
    End If

    If p2 <> "" Then
        Text2.Text = Text2.Text * (1 - (1 / p2))
    End If

    If p3 <> "" Then
       Text2.Text = Text2.Text * (1 - (1 / p3))
    End If

    If p4 <> "" Then
       Text2.Text = Text2.Text * (1 - (1 / p4))
    End If

End Sub



My end goal of all this is to make a program that will give me the PHI of a number the user enters, and that it will generate primes itself and work out + display the prime factors as well as the answer.... i have a Java Script version of this (well it gives the PHI, but doesnt work out primes etc) and i am trying to convert it into a fast and efficient VB program)

Here is the JS code incase it illustrates the end goal a little better:

<script type="text/javascript">
 function phi(n){
  facts=0
  for (i=1;i<n;i++){
  common=1
 for (j=2;j<=i;j++)
 if ((i%j==0) && (n%j==0)) common=0
facts+=common
 }
return facts
}
 i = 2
 do
{
document.write("PHI")
 document.write(i)
  document.write("  =  ")
   document.write(phi(i))
    document.write("<br/>")
i++
}
while (i <= 1000)
</script>
0
mlmccCommented:
You can't use lngfactor as the loop invariant and the final value. Also IsPrime returns True/False should be boolean

Function IsPrime(lngNumber As Long) As Boolean
    Dim i as Long
    Dim lngFactor As Long
    IsPrime = True
    lngFactor = Int(Sqr(lngNumber))
    If lngNumber > 3 Then
          If lngNumber Mod 2 = 0 Then
            IsPrime = False
          Else
            For i = 3 To lngFactor Step 2
              If lngNumber Mod i = 0 Then
                IsPrime = False
                Exit For
              End If
            Next i
          End If
    End If
End Function

mlmcc

0
cofneverlivetotellAuthor Commented:
Still not working, VB freezes when i try to run.  Im just going to accept this question and be done with it, no time to work on further as i am getting a lot of other work in school at the minute and exams are in 2 weeks.

Thanks for your help anyway

Cheers
Cof
0
mlmccCommented:
Glad i could help

mlmcc
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.