Math based C#

Incrementation

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

49 Replies - 4517 Views - Last Post: 20 October 2010 - 01:56 PM Rate Topic: -----

#1 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Math based C#

Posted 16 October 2010 - 05:59 AM

Hey guys im not really strong on maths but trying to do a simple console app here.

I need to increment a number so it starts at 10 then 40 then 90, 160 and so on the math behind it is always adding +20 to the difference before hand 10-40 = 30 + 20 = 50 + 40 = (90).

I have some code but dont know how to do this kind of incrementing calculation? :S

            for (int I = 0; I <= 4000;)
            {
                I = I (calculation needed);
                Console.WriteLine(I);
                Console.ReadLine();

This post has been edited by Sparukus: 16 October 2010 - 06:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Math based C#

#2 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Math based C#

Posted 16 October 2010 - 06:13 AM

View PostSparukus, on 16 October 2010 - 04:59 AM, said:

Hey guys im not really strong on maths but trying to do a simple console app here.

I need to increment a number so it starts at 10 then 40 then 90, 160 and so on the math behind it is always adding +20 to the difference before hand 10-40 = 30 + 20 = 50 + 40 = (90).

I have some code but dont know how to do this kind of incrementing calculation? :S

            for (int I = 0; I <= 4000;)
            {
                I = I (calculation needed);
                Console.WriteLine(I);
                Console.ReadLine();


Are you looking for one final value or are you looking for the sequence of answers?

Can you explain more clearly what you are trying to do aswell. The starting sequence of number such as 10,40,93,160 do you now all of these values before hand?

This post has been edited by mouse88: 16 October 2010 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Math based C#

Posted 16 October 2010 - 06:19 AM

just looking to output those sequence of numbers up untill 4000.

I know there should just be a simple math equation that I can use just dont know what it is yet :S

something like I = I + the diference between the last two numbers + 20.
Was This Post Helpful? 0
  • +
  • -

#4 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Math based C#

Posted 16 October 2010 - 06:31 AM

I know them before hand yes but I want to use a calculation to out put rather than manually having to type them all out ofcourse.

I could do

for (int I = 20; I <= 4000;)


then all I have to do is calculate the difference between the last two numbers

I = I + (the difference);

Was This Post Helpful? 0
  • +
  • -

#5 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Math based C#

Posted 16 October 2010 - 06:35 AM

View PostSparukus, on 16 October 2010 - 05:31 AM, said:

I know them before hand yes but I want to use a calculation to out put rather than manually having to type them all out ofcourse.

I could do

for (int I = 20; I <= 4000;)


then all I have to do is calculate the difference between the last two numbers

I = I + (the difference);


So wheres the 20 come from then? Im a bit confused as to what you are trying to do. In your above code you will be looping your code 3980 times. Is this what you should be doing?

This post has been edited by mouse88: 16 October 2010 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Math based C#

Posted 16 October 2010 - 06:39 AM

me too! lol

Ok im trying to create a sequence of numbers from 10 to 40 to 90 to 160 and SO forth up to 4000.

There is a calculation for incrementing that specific pattern. I just dont know what it is yet...

Im trying to keep it simple:

If you put this in a console app and run it and keep pressing return you will see that the number is incremented I want to do that but with my own specific patter as mentioned above. I just dont know how to.

            for (int I = 0; I <= 4000;)
            {
                I = I + 50;
                Console.WriteLine(I);
                Console.ReadLine();
        }

Was This Post Helpful? 0
  • +
  • -

#7 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Math based C#

Posted 16 October 2010 - 06:43 AM

View PostSparukus, on 16 October 2010 - 05:39 AM, said:

me too! lol

Ok im trying to create a sequence of numbers from 10 to 40 to 90 to 160 and SO forth up to 4000.

There is a calculation for incrementing that specific pattern. I just dont know what it is yet...

Im trying to keep it simple:

If you put this in a console app and run it and keep pressing return you will see that the number is incremented I want to do that but with my own specific patter as mentioned above. I just dont know how to.

            for (int I = 0; I <= 4000;)
            {
                I = I + 50;
                Console.WriteLine(I);
                Console.ReadLine();
        }


Ok well your code will need to know the next number in the sequence so that it can calculate the difference
Was This Post Helpful? 0
  • +
  • -

#8 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Math based C#

Posted 16 October 2010 - 07:10 AM

Nah this one is beyond me I think head isnt working today lol. Altho that Fibornacci thing might hold a clue.
Was This Post Helpful? 0
  • +
  • -

#9 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Math based C#

Posted 16 October 2010 - 07:12 AM

Hello there,

I would suggest that you could use the formula 10n^2 (or 10*n*n) for this sequence. Therefore, I would have thought that this would do the trick:

namespace Sequence
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 20; i++)
            {
                Console.WriteLine(10*(i*i)); //10*i*i is all that is needed!
            }

            Console.ReadLine(); //ensures console doesn't close upon completion
        }

    }
}



This is quite a simple sequence actually. The forumla basically allows you to find the nth term of this particular sequence. Or, put another way, it allows you to find ANY term in that sequence (and only that particular sequence). For example, if you want to find the 30th term in that sequence, you replace 'n' in the formula, with 30. Therefore, the 30th term in the sequence you have given would be 10*30*30 (from 10*n^2), which I think is 9000. So, the 30th term in the sequence will be 9000. You can therefore use this formula to greatly simplify your code.

Hope this helps. Don't hesitate to ask if you have any other queries or need any clarification :).

This post has been edited by CodingSup3rnatur@l-360: 16 October 2010 - 07:22 AM

Was This Post Helpful? 2
  • +
  • -

#10 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Math based C#

Posted 16 October 2010 - 07:16 AM

Actually, this is so simple. You just have to make a variable to hold the last number.
    int last = 0;
    for (int I = 0; I <= 4000;)
    {
        int newint = 0;
        newint = I + last; //Note: you can't set I as it is a constant variable.
        Console.WriteLine(newint);
        Console.ReadLine();
        last = I;
    }


Was This Post Helpful? 1
  • +
  • -

#11 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Math based C#

Posted 16 October 2010 - 07:21 AM

that works like a charm. Thanks man. But how is it working lol

How did you figure that out.... is seriously scratching is head!

View PostAmrykid, on 16 October 2010 - 06:16 AM, said:

Actually, this is so simple. You just have to make a variable to hold the last number.
    int last = 0;
    for (int I = 0; I <= 4000;)
    {
        int newint = 0;
        newint = I + last; //Note: you can't set I as it is a constant variable.
        Console.WriteLine(newint);
        Console.ReadLine();
        last = I;
    }



erm this doesnt work friend you only get a sequence of 0's.
Was This Post Helpful? 0
  • +
  • -

#12 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Math based C#

Posted 16 October 2010 - 07:42 AM

If you're applying that code, your entering in an infinite loop:

Your 'last'-variable is 0 in the beginning.

In the loop it is still 0, together with your 'newint'-variable. The sum of the 'newint' and the 'last'-variable results in 0 and is then stored in 'I'

Your result will be 0 at any time, therefore you need to set your last variable bigger than 0

Hoped this helps you out ^^

[though, I am kinda curious about this problem, maybe I'll figure out something this evening, I am kinda busy now]
Was This Post Helpful? 1
  • +
  • -

#13 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Math based C#

Posted 16 October 2010 - 08:04 AM

View PostSparukus, on 16 October 2010 - 06:21 AM, said:

that works like a charm. Thanks man. But how is it working lol

How did you figure that out.... is seriously scratching is head!

View PostAmrykid, on 16 October 2010 - 06:16 AM, said:

Actually, this is so simple. You just have to make a variable to hold the last number.
    int last = 0;
    for (int I = 0; I <= 4000;)
    {
        int newint = 0;
        newint = I + last; //Note: you can't set I as it is a constant variable.
        Console.WriteLine(newint);
        Console.ReadLine();
        last = I;
    }



erm this doesnt work friend you only get a sequence of 0's.


Hello again,

Yeah, don't worry about it. I will explain what I did in the context of this example, without going into too much detail on sequences in general (as there are quite a few formulae for different scenarios. I will try and explain it, but it may be difficult to explain.

Anyway, with your particular sequence, it is possible to just spot the pattern without doing anything. However, to work out the formula that I quoted, you do the following:

Firstly, you take the first term of the sequence. In this example it is 10. In sequences, the letter 'a' is usually used to denote the first term. So we can say a=10.

Secondly, we find the difference between each of the terms. So, in your example, we had:

10,40,90,160

The differences between these values are as follows:

30 (40-10) , 50 (90 - 40) , 70 (160 - 90).

However, these difference values are clearly NOT the same. We want to get difference values that ARE the SAME.

Therefore, we find the difference between each of the difference values (if that makes any sense at all!), as follows:

20 (50 - 30) , 20 (70 - 50)

Now, as you can see, we have a COMMON DIFFERENCE (20 in this case). Now we have all we need.

Right, now we use the following formula a + d(n-1)+1/2c(n-2)(n-1).

That may look awful, but it is actually quite simple. We already know a (it is the first term of the sequence which, in this case, is 10). d, in this formula, is the very first difference value we calculated. So, in this case, the very first difference was 40 - 10, which is 30. Therefore, d = 30. Now, c is the COMMON DIFFERENCE, which, is 20 in this case. n doesn't have a specific value at this point. n is just the letter that we replace to find a certain term. So, if we wanted to find the 10th term, we would replace n with 10, for example. n is a variable basically.

So, the formula, after we have put our numbers into becomes:

10 + 30(n-1) + 0.5*20(n-2)(n-1).

Now, we can simplify this as follows:

First, do 30(n-1). This equals 30n - 30. While we are here, we can do 0.5*20 also, which obviously equals 10. So, our formula is now:

10 + 30n - 30 + 10(n-2)(n-1).

Now we need to deal with the 10(n-2)(n-1). Firstly, I would multiply out 10(n-2) = 10n - 20 (in much the same way as we did 30(n-1) earlier.

Now we have 10 + 30n - 30 + (10n-20)(n-1). Now to deal with (10n-20)(n-1). I don't know if you know how we would multiply this out, but we use the word FOIL to remember how we do it. Basically, we multiply the first terms, then the outer terms, then the inner terms, then the last terms (FOIL = First Outer Inner Last) and add them together. So, in this case, we get:

10n^2 - 10n - 20n + 2. We can then do - 10n + - 20n to give: 10n^2 - 30n + 2.

Therefore, our overall formula looks like this now:

10 + 30n - 30 + 10n^2 - 30n + 20. Now, this is very easy to simplify:

First, we add all the lone numbers together: 10 + - 30 + 20. This equals zero, so the formula becomes:

30n + 10n^2 - 30n. However, we have a +30n and a - 30n. We add these together, and they equal zero.

Therefore, we are left with 10n^2, with will find any term in the sequence for you.

I know that may seem really complicated (I probably haven't helped with the way I have explained it), but it is very simple when you get the hang of it. I think we covered this at GCSE at school.

It is important to note though, this formula (a + d(n-1)+1/2c(n-2)(n-1)) will only work if you get a common difference on the second attempt. For example, in our example, you may remember that we found the difference of the original terms in the sequence, but the differences were all varied (they were not the same value; we didn't have a common difference). Therefore, we had to take the difference AGAIN, to get a common difference. This is what I mean by taking the difference twice. This is the formula for when you get the common difference of a formula on your second attempt.

There are different formulae for when you reach a common difference one the first attempt, third attempt etc. Also, in many sequences, you can (and may have to), use other formulae.

I would suggest that if you really want to know about all these different sequences and formulae etc, you have a good scout around the Internet and perhaps buy a few student maths books too.

Anyway, sorry for the massive post. It is a very difficult thing to explain (I have just found out!), and I am sorry if it was difficult to understand. That was only a tiny tiny part of sequences. There are many other sequences that this formula will not work on. You just have to learn about these yourself to gain a full understanding :). This particular example is almost as simple as it gets really.

If you get this common difference idea down, then you may want to look at the formal definitions and associated formula of Arithmetic, Geometric and Linear Recurrence sequences. They are very simple once you 'get' them. WHen looking at these, look for 'recurrence systems' (allows you to find the next term in the sequence) and 'closed form' (allows you to find any term in the sequence). It gets easier when you think of sequences in these terms as Arithmetic, Geometric and Linear Recurrence sequences all have more simple, general formulae associated with them. Although, not every sequence matches any of these types...

I'm rambling... i'll leave you too it now :).

This post has been edited by CodingSup3rnatur@l-360: 16 October 2010 - 08:34 AM

Was This Post Helpful? 4
  • +
  • -

#14 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: Math based C#

Posted 16 October 2010 - 10:42 AM

omg man, you have scrambled my brain! That was way in depth. Yeah math wasnt a strong point for me, Its also been years since ive needed to use it.

coming back to this code tho:

                for (int i = 1; i <= 20; i++)
                {
                    Console.WriteLine(10*(i*i));
                }
     
                Console.ReadLine();


how does this know to get to 4000 what if I wanted to go higher? ive tryed changing all the variables but cant seem to go past 4000 or 20 lines in the console :S

wait got it:

                for (int i = 1; i <= 200; i++)
                {
                    Console.WriteLine(10*(i*i));
                }
     
                Console.ReadLine();


forgot the scroll bar lol
Was This Post Helpful? 0
  • +
  • -

#15 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Math based C#

Posted 16 October 2010 - 11:11 AM

View PostSparukus, on 16 October 2010 - 09:42 AM, said:

omg man, you have scrambled my brain! That was way in depth. Yeah math wasnt a strong point for me, Its also been years since ive needed to use it.

coming back to this code tho:

                for (int i = 1; i <= 20; i++)
                {
                    Console.WriteLine(10*(i*i));
                }
     
                Console.ReadLine();


how does this know to get to 4000 what if I wanted to go higher? ive tryed changing all the variables but cant seem to go past 4000 or 20 lines in the console :S

wait got it:

                for (int i = 1; i <= 200; i++)
                {
                    Console.WriteLine(10*(i*i));
                }
     
                Console.ReadLine();


forgot the scroll bar lol


Yeah, I can see it looks a bit complicated, particuarly if you haven't done any maths for a while :). Anyway, you are exactly right with the second bit of code. You just change the condition value in the loop. Think of the variable 'i' in the loop as being 'n' in my formula (10*n*n). All the loop is doing is going through each term one by one and calculating it. First time round, 'i' (and n therefore) is one, so it calculates the first term. Then, 'i' gets incremented by 1, so the loop now calculates the 2nd term, and it goes on like this until it reaches the number you provide in the conditional part of the loop. Therefore, in your example, the sequence will be displayed up to and including the 200th term!

This post has been edited by CodingSup3rnatur@l-360: 16 October 2010 - 11:16 AM

Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »