• 0

Trouble with C# algorithm


Question

Hi guys, I am trying to find the best possible period in a given array filled with random doubles including negative numbers.

each number in the array represents a daily stock gain(loss). And the array size is 365 representive of the number of days in a year. My goal is to loop trough this array and find the best period (sequence of numbers that add up to the highest) within the array. I am having difficulty coming up with an algorithm and was wondering if anyone here may be of help. Thanks. :(

Here is my code, but it doesn't work right, I get something like 7390 when I am suppose to get 1440. I can provide the txt file with the numbers in the array if you like.

private double calculateBestGainsPeriod()
	{
 ?double[] mydailyGains = this.dailyGains;
 ?double possibleGain=0.0;
 ?double gainsSoFar=0.0;

 ?for(int i = 0; i < dailyGains.Length; i++)
 ?{
 ?	if(dailyGains[i] > 0)
 ?	{
 ? ?gainsSoFar+= dailyGains[i];
 ?	}
 ?	else if(dailyGains[i] < 0 && i < dailyGains.Length-1)
 ?	{
 ? ?//negativeGain = dailyGains[i];
 ? ?possibleGain = gainsSoFar + dailyGains[i] + mydailyGains[i+1];
 ? ?if(possibleGain > gainsSoFar)
 ? ?{
 ? ?	gainsSoFar = possibleGain;
 ? ?}
 ? ?possibleGain = 0;
 ? ?i++;
 ?	}
 ?}
 ?return gainsSoFar;

	}

GainsReport.txt

Edited by Dieot
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

max[3] = {0,0,0}

// Try all length possible
for (length = 1; length<=365; length++) {
 ? ?// For each length, try all intervals possible
 ? ?for (pos=length-1; pos < 365; pos++) {
 ? ? ? ?sum = 0
 ? ? ? ?// For each interval, calculate the sum
 ? ? ? ?for (j=pos; j > (pos-length); j--) {
 ? ? ? ? ? ?sum += array[j]
 ? ? ? ?}
 ? ? ? ?// If that sum is bigger than the max found to date, u found a new max period
 ? ? ? ?if (sum > max[0]) {
 ? ? ? ? ? ?max[0] = sum
 ? ? ? ? ? ?max[1] = pos-length+1
 ? ? ? ? ? ?max[2] = length
 ? ? ? ?}
 ? ?}
}

// Here, max[0] is the max you you can get for any period
// max[1] is where in the year this period starts
// max[2] is how long the period is

// untested algorithm... but should work fine :)

Had some time, so I tried my algorithm in java.

Max Period: 1463.84 starting at 118 of length 240

Takes less than 2secs to find the;)nswer, with funny little dots to show the progress! ;)

Edited by Mouton
Link to comment
Share on other sites

  • 0

i just wanna clarify what you mean by the best sequence

does this mean for example

6,5,4,5,6,-1,4,5,6,7,7,-2,4,5,6,7

for the above data set, when we sum up the values 6,5,4,5,6, and since the next value is a negative value, does this small set, i.e. 6,5,4,5,6 represent a sequence? i'm just not sure what you mean a sequence here is, does a negative value indicate the sequence ends there, and then the next positive number is the start of a new sequence?

Link to comment
Share on other sites

  • 0

From what I understood, the numbers are money.

For all days, the number is either the amount of money a particular stock gained or lost during this day.

He's trying to find what's the most money you could have done with this stock during this year, if u had bought it at day x and sold it at day y, with x and y between 1 and 365.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.