SRL Archive - General

Polynomial degree solver


n3ss3s

2011-04-30 16:18:55

In math at the moment we are finding out degrees of polynomials through finding their differences. Basically what it means is, if the first set of differences between values are equal to each other, it's a first degree polynomial, etc.

I made this because I got a worksheet with a crapton of this for homework, and doing it by hand takes a couple minutes. What you want to keep in mind, you need to have a big enough array of starting values, because if you reach the end of the pyramid (only one difference left) without finding multiple equal difference, it's not a correct result. Please don't try to find the degree of a polynomial with two or three base values as that's just retarded.


Hope someone else is at the same point in math :)

C#

[PHP]/*
* Created by SharpDevelop.
* User: admin
* Date: 30.4.2011
* Time: 14:37
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/

using System;
using System.Collections.Generic;

namespace Differences
{
class Program
{
public static bool Singular(int[] arr) // I know Singular isn't the correct term but oh well…
{
int x = arr[0];
for (int i = 1; i < arr.Length; i++)
if (arr != x)
return false;
return true;
}

public static void Main(string[] args)
{
Console.WriteLine("Ready"+System.Environment.NewLine);
bool found = false;
while (true)
{
string s = Console.ReadLine();
if (s.Equals("help") || (!s.Contains(":"))) {
Console.WriteLine(System.Environment.NewLine);
Console.WriteLine("Format: a, b, c, d:MaxDegree - 1, 2, 3, 4:5");
Console.WriteLine(System.Environment.NewLine);
continue;
}
if (s.Equals("exit"))
return;
Console.WriteLine(System.Environment.NewLine);
found = false;
string[] sp = s.Split(':');
string[] dig = sp[0].Split(',');
List<int> bases = new List<int>();
foreach (string x in dig)
{
int n = -1;
if (Int32.TryParse(x, out n))
bases.Add(n);
}
if (Singular(bases.ToArray()))
{
System.Console.WriteLine("*** NOT A POLYNOMIAL ***");
Console.WriteLine(System.Environment.NewLine);
continue;
}
int maxd = Int32.Parse(sp[1]);
for (int c = 0; c < maxd; c++)
{
List<int> newset = new List<int>();
for (int a = 0; a < bases.Count - 1; a++)
{
int dif = (-1) * (bases[a] - bases[a + 1]);
newset.Add(dif);
Console.Write(dif + " ");
}
Console.WriteLine(System.Environment.NewLine);
if (Singular(newset.ToArray()))
{
if (newset.Count == 1) {
Console.WriteLine("*** END OF PYRAMID - TRY ADDING MORE BASE VALUES ***");
break;
}
Console.WriteLine("Degree: " + (c + 1) + " Last Difference: "+newset[0]);
Console.WriteLine(System.Environment.NewLine);
found = true;
break;
}
bases.Clear();
bases = newset;
}
if (!found)
Console.WriteLine("The degree is higher than "+maxd+" or you need a larger array.");
}
}
}
}[/PHP]