Finding largest number with no Diophantine representation (in Python)

  • Context: Python 
  • Thread starter Thread starter ephedyn
  • Start date Start date
  • Tags Tags
    Python Representation
Click For Summary

Discussion Overview

The discussion revolves around writing a Python program to find the largest integer \( n \) such that the equation \( 6a + 9b + 20c = n \) has no nonnegative integer solutions for \( a, b, c \). Participants explore coding strategies, debugging issues, and program structure related to this problem.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach using nested loops but struggles with termination conditions, indicating that the loops run indefinitely.
  • Another participant suggests breaking the program into smaller, manageable parts and emphasizes the importance of testing each part before integration.
  • A suggestion is made to define a function called "hasSolution(n)" to check for nonnegative integer solutions, proposing a "divide and conquer" strategy to simplify the main program.
  • Concerns are raised about the logic in the provided code snippet, particularly regarding the exit condition of the while loop and the initialization of variables.

Areas of Agreement / Disagreement

There is no consensus on the best approach to solve the problem, and multiple strategies are proposed without agreement on which is superior. Participants express differing views on how to structure the program and address the coding issues.

Contextual Notes

Participants have not fully resolved the logical issues in the code, and there are uncertainties regarding the termination conditions of the loops and the overall program structure.

Who May Find This Useful

This discussion may be useful for individuals interested in programming challenges related to number theory, algorithm design, or those seeking to improve their Python coding skills through collaborative problem-solving.

ephedyn
Messages
169
Reaction score
1

Homework Statement


Write an iterative program to find the largest integer n such that the equation

6a + 9b + 20c = n

where a, b, c are nonnegative integers, has no solution.

Homework Equations



We are given that the set of n in {x, x+1, x+2, ..., x+5} has nonnegative solutions a, b, c to the equation 6a + 9b + 20c = n if and only if it is possible to find solutions for all n ≥ x. In other words, we should loop our program until we have a list of 6 consecutive integer values, and the first member of the previous list of values stored (which fails this test of consecutive integers) is the desired solution.

We are also given the example that n = 50, 51, 52, 53, 54, 55 have solutions, which is nice, so the program shouldn't need to compute for a very long time.

The Attempt at a Solution



I did something like this, with five nested while loops (I truncated the remaining two so you guys don't have to bear with the pain of reading horrible code haha.) The issue I'm facing is that the nested loop will just go on forever. I can't figure the way to make it terminate at the smallest possible increment from solutionSet[5] before going on to the next nested loop and so on. (In the first test case, the nested loops should terminate when they return 9, 12, 15, 18, 21.) The conditional test of the outermost loop is also messed up, because when I get my desired [9,12,15,18,21] as the first set, it will just terminate there.

Code:
solutionSet= [1, 2, 3, 4, 5, 6]
while solutionSet == range(solutionSet[0],solutionSet[5]+1)

	a = 0;
	b = 0;
	c = 0;
	index = 0;
	x = 0;

	while x != solutionSet[5]+1
		cycle = index % 4;
		if cycle == 1
			a = a+1;
		elif cycle == 2
			b = b+1;
		elif cycle == 3
			c = c+1;
		index = index + 1;
		x = 6*a + 9*b + 20*c;
		solutionSet[0] = x;

	a = 0;
	b = 0;
	c = 0;
	index = 0;
	x = 0;

	while x != solutionSet[5]+2
		cycle = index % 4;
		if cycle == 1
			a = a+1;
		elif cycle == 2
			b = b+1;
		elif cycle == 3
			c = c+1;
		index = index + 1;
		x = 6*a + 9*b + 20*c;
		solutionSet[1] = x;

	a = 0;
	b = 0;
	c = 0;
	index = 0;
	x = 0;

	while x != solutionSet[5]+3
		cycle = index % 4;
		if cycle == 1
			a = a+1;
		elif cycle == 2
			b = b+1;
		elif cycle == 3
			c = c+1;
		index = index + 1;
		x = 6*a + 9*b + 20*c;
		solutionSet[2] = x;

	...
	...

Any tips?

Thanks!
 
Technology news on Phys.org
Anyone? (:
 
There is only one way to author a good program, and that's to write it in small manageable parts, testing each to confirm it will do what you intend, before combining into something larger.

First, let me say I don't exactly follow what the goal of the final program is, but let's look at a snippet of your code:
Code:
    while x != solutionSet[5]+1
		cycle = index % 4;
		if cycle == 1
			a = a+1;
		elif cycle == 2
			b = b+1;
		elif cycle == 3
			c = c+1;
		index = index + 1;
		x = 6*a + 9*b + 20*c;
		solutionSet[0] = x;

On the first transit through this, nothing is incremented except "index", so the value of x is set to 0, and solutionSet[0] is also set to 0.
On the second loop through, "a" becomes 1, so x jumps up to 6.
On the third loop, "b" is incremented, making x jump to 15, and so on.
Are you expecting that sooner or later x will become equal to 7? Because if it doesn't, then it is going to miss the exit condition and this "while loop" will never terminate.
 
ephedyn said:
Anyone? (:
How about you start by defining a function "hasSolution(n)" to test if the equation has a suitable solution.
Code:
def hasSolution(n) :
# Brute force search for non-negative integer solution to 6a + 9b +20c = n
# Takes integer. Returns boolean.
    ...
    return ...   # True or False

This "divide and conquer" approach of writing the important elements of your task as functions let's you easily test the partial code. It will also make the main body of your program much simpler and easier to understand.
 

Similar threads

  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K