Math Problem/Algorithm - Page 1
Forum section: electronics / general

author: metrologist
author_rank: Super Contributor
timestamp: July 09, 2025, 06:22:34 am
content: Perhaps posed more of a computer program algorithm...for 2 players. I think this was the way it went...There is a series of random numbers. The players alternately select a number from either end of the list until all numbers have been selected. The player with the highest sum wins.Proposed solutions:Add the first and second to last number; add the second and last number; whichever sum is higher tells you from which end to select your number.No solution because it depends on who goes first or there are other variables that dominate.Other solution.

author: u666sa
author_rank: Frequent Contributor
timestamp: July 09, 2025, 02:31:07 pm
content: You radomize and then get random number.You'd have stack for each player. You'd push random number to one stack, switch to other stack, get random number and push it there, and then rinse and repeat however many random numbers you want to have for each player time two.Then you pop from stock and add all numbers up. Do this for each player. Whoever has highest sum wins.It's as simple as that.There is a catch however, that sum variable has to be big enough to hold entire sum. int64 at least.

author: ledtester
author_rank: Super Contributor
timestamp: July 09, 2025, 02:48:41 pm
content: I think you can use dynamic programming.Suppose the numbers are a, a, ..., a.Let S[i,j] (where i <= j) be the best way to play the game on the numbers a[\i], a[i+1], ..., a.Given you know S[i,j] you can find the best plays for intervals which are one larger, i.e. S[i-1,j] and S[i,j+1].So you only have to solve a quadratic number of boards.

author: EPAIII
author_rank: Super Contributor
timestamp: July 11, 2025, 07:47:35 am
content: Random numbers? The problem seems to be very vaguely stated.Are they sequential? Or with random gaps? Or regular gaps?Can the players view all of the randomized list before beginning? If not, what parts?Does the list have an even or an odd number of numbers?I wouldn't waste any time on creating an algorithm until all the details are fully stated. Who knows which one may be important.

author: metrologist
author_rank: Super Contributor
timestamp: July 14, 2025, 09:32:45 pm
content: It's as ledtester stateda, a, ..., awhere each a is a random number, and n is also a random number.So in programming, I assume the list is an array of random numbers, and the programmatic algorithm just has to deal with what it's fed.

author: Nominal Animal
author_rank: Super Contributor
timestamp: July 14, 2025, 10:50:08 pm
content: Quote from: metrologist on July 09, 2025, 06:22:34 amThe key question is whether each player sees the entire list, a partial list, or just the currently available two numbers. That determines whether you need anoffline(sees everything) or anonline(only sees the values to choose from) algorithm.Agreedy algorithmtakes the option that yields the best immediate result. These are typically only used when it can be mathematically proven that it leads to an optimum or an acceptable solution for all possible inputs, but can also be used to implement "strategically easily beatable" computer opponents.Abrute force algorithmexamines all possible ways the rest of the game could proceed, and chooses the "best" option among those. "Best option" is the one that maximizes/minimizes some scoring criteria; here, it could be the difference in scores for the two players, or perhaps the number of sub-paths where the computer loses.For learning purposes, you'll want to implement more than two algorithms, with differentstrategies, and let each unique pair of algorithms play a set number of randomized games against each other. Record the scores separately based on which algorithm made the first choice, how many rounds it took, and what the difference in scores was. You'll learn a lot about this particular "game" at hand.To learn how to apply mathematics to find a winning strategy for a player algorithm, look intocombinatorial game theory, and at least initially assuming the players can see all numbers left when making their next choice.

author: John Coloccia
author_rank: Super Contributor
timestamp: July 14, 2025, 11:44:17 pm
content: I feel like there is more to the original problem. If it's truly just random numbers, and it's turn based, and you know nothing else, and once a slot is picked it's gone, I believe there is no strategy. The list is irrelevant because the number is random, so it may as well be just a random number from wherever.

author: metrologist
author_rank: Super Contributor
timestamp: July 15, 2025, 12:36:02 am
content: The program is fed the entire list, so the entire array can be accessed by the program.Looks like the solution I posted is almost like a greedy algorithm but not really.

author: John Coloccia
author_rank: Super Contributor
timestamp: July 15, 2025, 12:46:58 am
content: Quote from: metrologist on July 15, 2025, 12:36:02 amBrute force would be to created a weighted, binary graph. 0 at the top. Left/right corresponds to turn.You can make it more memory effecient by culling paths which are definite loosers....no reason to explore beyond that. But that's a pretty heavy handed algorithm.But for sufficiently small lists, it works. At least I think it does... Lol.I've only thought about this for about 30 seconds. It's there a better way? Seems like an interesting problem!

author: u666sa
author_rank: Frequent Contributor
timestamp: July 17, 2025, 03:02:17 pm
content: Quote from: metrologist on July 14, 2025, 09:32:45 pmDoes n a random number at the start of the program or is it a random number each time we loop?

author: metrologist
author_rank: Super Contributor
timestamp: July 17, 2025, 07:27:57 pm
content: The list of numbers is finite and static at the start.

author: u666sa
author_rank: Frequent Contributor
timestamp: July 17, 2025, 08:27:18 pm
content: So we haveCode:static int n = random();int64 resultA = 0;int64 resultB = 0;std:stack<int> stackA, stackB;std::srand(static_cast<unsigned int>(std::time(nullptr)));for (int i=0; i<n; i++) {stackA.push( std::rand() );stackB.push( std::rand() );}for (int i= n-1; i > -1; i--) {stackB.pop() + whatever math here to determine who wins}