If If statement is false then continue program flow

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 3365 Views - Last Post: 11 November 2012 - 07:42 PM Rate Topic: -----

#1 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

If If statement is false then continue program flow

Posted 07 November 2012 - 09:38 AM

Hey, im doing a little program to find couple of numebsr where the factorial of the first = the square power of the second -1, for example 4 and 5, 24 and 25. Im finding a problem that instead of the program to find one of these couples and move on to find others it simply ends. How can i make him continue?
Code for now:
Imports System.Numerics
Module Module1
    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 2

    Sub Main()
        Console.ForegroundColor = ConsoleColor.Yellow
        For n As BigInteger = 1 To (count * count)
            For c As BigInteger = 1 To n
                fact = fact * c
            Next
            square = n + 1
            If ((square * square) - 1 = fact) Then
                Console.Write("Found! ")
                Console.Write((count - 1))
                Console.Write(",")
                Console.Write(square)
            End If
            fact = 1
            count = count + 1
        Next


    End Sub

End Module


Is This A Good Question/Topic? 1
  • +

Replies To: If If statement is false then continue program flow

#2 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1534
  • View blog
  • Posts: 4,930
  • Joined: 25-September 09

Re: If If statement is false then continue program flow

Posted 07 November 2012 - 09:45 AM

If ((square * square) - 1 = fact) Then
If ^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^ is true is what that is saying.


If you don't want to react when (square*square)-1 = fact you could simply use
If Not ((Square * square) - 1 = fact) Then

Or
If ((square * square) -1 <> fact) Then

<> is Not Equal

An if statement reacts to a true condition

This post has been edited by CharlieMay: 07 November 2012 - 09:46 AM

Was This Post Helpful? 1
  • +
  • -

#3 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 07 November 2012 - 09:53 AM

Sorry, i think i made my question in the wrong way. The problem is, when he finds the 2 numbers, like 4 and 5 it prints them on the console and then the program stops, i want that when he find a pair of numbers (like 4 and 5) it prints them and then continue to find other numbers like those, instead of stopping like he is doing now.
Was This Post Helpful? 0
  • +
  • -

#4 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 07 November 2012 - 10:55 AM

Can anyone help?
Was This Post Helpful? -1
  • +
  • -

#5 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 07 November 2012 - 11:39 AM

View PostRaminator, on 07 November 2012 - 10:55 AM, said:

Can anyone help?

Please, i really need help with this.
Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: If If statement is false then continue program flow

Posted 07 November 2012 - 01:44 PM

Not sure if I can help with this or not.

I don't understand why you have count=2, then in the For loop, you increment c from 1 to count*count, then increment count by 1 each time through the For Loop.

Math isn't exactly my strong suit (my brother, the mathematician, wrote books that lost me on the title page), so I can't tell if there is a reason for looping from 1 to 16, and incrementing count (used only to set the loop count, and for outputting on the Console.

I have made a few changes to your code, making it do what I THINK you want to accomplish. The changes I made are commented in the following code:

    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 1000 ' test all numbers up to 1000. Note: these factorials get VERY big, VERY fast.

    Private Sub Main()
        Dim c As BigInteger    ' Dim here because we need to use it after the "For c ..." loop
        Console.ForegroundColor = ConsoleColor.Yellow
        For n = 1 To count
            For c = 1 To n
                fact = fact * c
            Next
            square = n + 1
            'Console.Write("fact: ")  ' These are commented out, but can be uncommented for troubleshooting
            'Console.Write(fact)
            'Console.Write(" , ")
            'Console.Write("(square*square)-1)")
            'Console.WriteLine((square * square) - 1)
            'Console.WriteLine(n)
            If ((square * square) - 1 = fact) Then
                Console.Write("Found! ")
                Console.Write((c - 1))  ' changed from count to c, because after the "For c ..." loop
                                        '   c is 1 higher than n
                Console.Write(",")
                Console.WriteLine(square)
            End If
            fact = 1
            'count = count + 1   'removed, as I see no use for it.
        Next
        Console.WriteLine("Done")
    End Sub



I would be very interested to know if there are any numbers that fit the criteria past 4. This code finds only one number that fits, and that's 4. I'm going to leave it running a while with a count of 5000 and see if it finds more.

This post has been edited by lar3ry: 07 November 2012 - 01:47 PM

Was This Post Helpful? 1
  • +
  • -

#7 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 09:07 AM

View Postlar3ry, on 07 November 2012 - 01:44 PM, said:

Not sure if I can help with this or not.

I don't understand why you have count=2, then in the For loop, you increment c from 1 to count*count, then increment count by 1 each time through the For Loop.

Math isn't exactly my strong suit (my brother, the mathematician, wrote books that lost me on the title page), so I can't tell if there is a reason for looping from 1 to 16, and incrementing count (used only to set the loop count, and for outputting on the Console.

I have made a few changes to your code, making it do what I THINK you want to accomplish. The changes I made are commented in the following code:

    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 1000 ' test all numbers up to 1000. Note: these factorials get VERY big, VERY fast.

    Private Sub Main()
        Dim c As BigInteger    ' Dim here because we need to use it after the "For c ..." loop
        Console.ForegroundColor = ConsoleColor.Yellow
        For n = 1 To count
            For c = 1 To n
                fact = fact * c
            Next
            square = n + 1
            'Console.Write("fact: ")  ' These are commented out, but can be uncommented for troubleshooting
            'Console.Write(fact)
            'Console.Write(" , ")
            'Console.Write("(square*square)-1)")
            'Console.WriteLine((square * square) - 1)
            'Console.WriteLine(n)
            If ((square * square) - 1 = fact) Then
                Console.Write("Found! ")
                Console.Write((c - 1))  ' changed from count to c, because after the "For c ..." loop
                                        '   c is 1 higher than n
                Console.Write(",")
                Console.WriteLine(square)
            End If
            fact = 1
            'count = count + 1   'removed, as I see no use for it.
        Next
        Console.WriteLine("Done")
    End Sub



I would be very interested to know if there are any numbers that fit the criteria past 4. This code finds only one number that fits, and that's 4. I'm going to leave it running a while with a count of 5000 and see if it finds more.

I added count = count +1 because this code is supposed to be endless ( i know crazy but). And i know the factorials get reaaaly big but bigintegers can handle really big numbers too so (also i always wanted to know the limit of bigintegers). Ill test your code but, do you have any idea why is the if statement not working properly on my code?
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 09:33 AM

View PostRaminator, on 08 November 2012 - 10:07 AM, said:

I added count = count +1 because this code is supposed to be endless ( i know crazy but). And i know the factorials get reaaaly big but bigintegers can handle really big numbers too so (also i always wanted to know the limit of bigintegers). Ill test your code but, do you have any idea why is the if statement not working properly on my code?


Well, count = count + 1 did absolutely nothing for you. You started by setting count to 2, and then your For Loop used count * count, which makes your loop count n from 1 to 4. after reaching 4, the loop drops out. The If statement has nothing to do with the program stopping.

Use my code, and uncomment the line just before the If statement that says Console.WriteLine(n), and you'll see it incrementing by 1, all the way to whatever value you put into count. My code sets it to 1000.

You will not find any other number that fits the criteria up to over 7000 or so, but that's as far as I've tested.

This post has been edited by lar3ry: 08 November 2012 - 09:34 AM

Was This Post Helpful? 1
  • +
  • -

#9 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 09:49 AM

View Postlar3ry, on 08 November 2012 - 09:33 AM, said:

View PostRaminator, on 08 November 2012 - 10:07 AM, said:

I added count = count +1 because this code is supposed to be endless ( i know crazy but). And i know the factorials get reaaaly big but bigintegers can handle really big numbers too so (also i always wanted to know the limit of bigintegers). Ill test your code but, do you have any idea why is the if statement not working properly on my code?


Well, count = count + 1 did absolutely nothing for you. You started by setting count to 2, and then your For Loop used count * count, which makes your loop count n from 1 to 4. after reaching 4, the loop drops out. The If statement has nothing to do with the program stopping.

Use my code, and uncomment the line just before the If statement that says Console.WriteLine(n), and you'll see it incrementing by 1, all the way to whatever value you put into count. My code sets it to 1000.

You will not find any other number that fits the criteria up to over 7000 or so, but that's as far as I've tested.

Ok, thanks. I did a little changes to your code so that now its endless and a little organization change. See what you think. I have been runing the code for like 30mins now.Attached Image
Imports System.Numerics
Module Module1
    Dim n As BigInteger = 1
    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 5000 ' test all numbers up to 1000. Note: these factorials get VERY big, VERY fast.
    Sub Main()
        Dim c As BigInteger    ' Dim here because we need to use it after the "For c ..." loop
        Console.ForegroundColor = ConsoleColor.Yellow
        For n As BigInteger = 1 To count
            For c = 1 To n
                fact = fact * c
            Next
            'Troubleshoot()
            square = n + 1
            If ((square * square) - 1 = fact) Then
                Console.Write("Found! ")
                Console.Write((c - 1))  ' changed from count to c, because after the "For c ..." loop
                '   c is 1 higher than n
                Console.Write(",")
                Console.WriteLine(square)
            End If
            fact = 1
            count = count * count   'removed, as I see no use for it.
        Next
        Console.WriteLine("Done")
    End Sub
    Private Sub Troubleshoot()
        Console.Write("fact: ")
        Console.Write(fact)
        Console.Write(" , ")
        Console.Write("(square*square)-1)")
        Console.WriteLine((square * square) - 1)
        Console.WriteLine(n)
    End Sub

End Module

Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 10:30 AM

View PostRaminator, on 08 November 2012 - 10:49 AM, said:

Ok, thanks. I did a little changes to your code so that now its endless and a little organization change. See what you think. I have been runing the code for like 30mins now.Attachment BN NOGUI2.png
Imports System.Numerics
Module Module1
    Dim n As BigInteger = 1
    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 5000 ' test all numbers up to 1000. Note: these factorials get VERY big, VERY fast.
    Sub Main()
        Dim c As BigInteger    ' Dim here because we need to use it after the "For c ..." loop
        Console.ForegroundColor = ConsoleColor.Yellow
        For n As BigInteger = 1 To count


You have already Dim'd n as BigInteger. You do not need to do it again. All you have done with this "As BigInteger" is to create a new variable, visible only in Sub Main(), and the one Dim'd atthe top is still there, using memory, but not being used for anything at all.

In a For loop, the terminating value (in this case, the value in count), is set at the time the For statement is executed. Changing its value within the For loop will not have any effect on the number of iterations of the loop. After executing the For loop, the variable n is set to the initial value (in thes case, 1), and changing the value of n within the loop will change the number of times the loop will execute. You really don't want to do that, because you want to check every number from 1 to count.

Quote

            For c = 1 To n
                fact = fact * c
            Next
            'Troubleshoot()
            square = n + 1
            If ((square * square) - 1 = fact) Then
                Console.Write("Found! ")
                Console.Write((c - 1))  ' changed from count to c, because after the "For c ..." loop
                '   c is 1 higher than n
                Console.Write(",")
                Console.WriteLine(square)
            End If
            fact = 1
            count = count * count   'removed, as I see no use for it.


You can leave that in there, if you really want, but I can assure you it does absolutely nothing. You are only using count to set a termination value in the For statement, and you never use it afterward. If you want it to run endlessly, you can essentially do it by setting count to a value of, let's say 500 million. It will probably run for longer than your lifetime, given a PC with enough storage, but will more likely run out of memory long before you get to that value.
Was This Post Helpful? 1
  • +
  • -

#11 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 11:20 AM

Im making a program to find Brown Numbers but its beeing harder then i thought it would be. I have an alpha version of it but it only finds brown numbers that are one followed by the other like (4,5) and i need it to find brown numbers even if they are not followed on by the other like (5,11) and (7,71). How can i do this? I hope my alpha code helps.
Code for now:
Imports System.Numerics
Module Module1
    Dim n As BigInteger = 1
    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 5000
    Sub Main()
        Dim c As BigInteger    ' Dim here because we need to use it after the "For c ..." loop
        Console.ForegroundColor = ConsoleColor.Yellow
        For n As BigInteger = 1 To count
            For c = 1 To n
                fact = fact * c
            Next
            'Troubleshoot()
            square = n + 1
            If ((square * square) - 1 = fact) Then
                Console.Write("Found! ")
                Console.Write((c - 1))  ' changed from count to c, because after the "For c ..." loop
                '   c is 1 higher than n
                Console.Write(",")
                Console.WriteLine(square)
            End If
            fact = 1
            count = count * count
        Next
        Console.WriteLine("Done")
    End Sub
    Private Sub Troubleshoot()
        Console.Write("fact: ")
        Console.Write(fact)
        Console.Write(" , ")
        Console.Write("(square*square)-1)")
        Console.WriteLine((square * square) - 1)
        Console.WriteLine(n)
    End Sub

End Module


This post has been edited by Raminator: 08 November 2012 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#12 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 11:31 AM

I'm going out for the day, but here's a way to do it (it will even find 4).

If the square root of (fact +1) is an integer (evenly divisible), then fact and the square root of fact + 1 is the other number.

This post has been edited by lar3ry: 08 November 2012 - 11:32 AM

Was This Post Helpful? 2
  • +
  • -

#13 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 11:39 AM

View Postlar3ry, on 08 November 2012 - 11:31 AM, said:

I'm going out for the day, but here's a way to do it (it will even find 4).

If the square root of (fact +1) is an integer (evenly divisible), then fact and the square root of fact + 1 is the other number.

Hmm sorry, but i didn't really get this.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10190
  • View blog
  • Posts: 37,635
  • Joined: 27-December 08

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 03:11 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#15 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 08 November 2012 - 04:19 PM

Can anyone help me with that problem?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3