# Thread: Problems on creating a sudoku verifier

1. ## Problems on creating a sudoku verifier

Hi there.I'm quite new to C and these boards.

I have a question on sudoku(9x9):

You'll input 81 digits like, 123412314523452345.......79687689.And the program will verify if these sequence is a valid sudoku board.It will suppose to read
1 2 3 4 1 2 3 1 4
5 2 3 4 5 2 3 4 5
and so on...
My first problem is that integers can't hold this value this large.What should I do to store this value?

I was thinking,in order to verify if it's a sudoku board,the program needs to add numbers in a row,column or a 3x3 box,and if the sum is not equal to 45,then the program will say that it is not a valid board.Is this algorithm right?Like if and else statements.

And in using arrays,do you add like a[0] + a[8]? How do you add from box 0 to box 8?

2. Read 81 digits as a string, then extract one digit at a time.

3. Sudoku solvers can use the 45, but few do. Most use the ONE rule of Sudoku - that each row, column or box, can have only one value of 1-9.

This clearly violates the one rule:
Code:
```1 2 3 | 4 1 2 | 3 1 4
5 2 3 | 4 5 2 | 3 4 5```
Don't even think about programming a "human" type of solver. Sudoku has so many subtleties to solving it the "human" way, that it's astonishing. That makes writing a robust human type of solver, extremely difficult. Think brute force solver.

4. I know how sudoku works.
It's just an example ,and I know that example is gonna make the program display that it's not a valid board.
I was talking about the sum of each row,column,and board.

Thanks for the replies.
Sorry if I don't know much about programming yet.

5. First, Welcome to the board, Twoxacross!

I've seen programs that used the 45 sum test, yes.

You don't need to apologize. Nobody is born able to program in C!

6. The problem with the 45 rule is that it doesn't work. "555555555" adds up to 45, but it isn't legal sudoku.

7. Exactly. The 45 sum can be used as a preliminary check, and as a final check for the last digit in a row, column, or box that is otherwise, legal. It can't be used as the only check, however.

Exactly. The 45 sum can be used as a preliminary check, and as a final check for the last digit in a row, column, or box that is otherwise, legal. It can't be used as the only check, however.
Why use two checks for the same thing when they need the same loop to run ?

9. Originally Posted by twoxacross
I was talking about the sum of each row,column,and board.

Thanks for the replies.
Sorry if I don't know much about programming yet.
You will need to sum the elements of the array up yourself using a loop. You should read: Lesson 3 - Loops and Lesson 8 -Arrays. These lessons will show you what you need to do.

10. Originally Posted by manasij7479
Why use two checks for the same thing when they need the same loop to run ?
Say you are trying the next candidate digit, for the last empty position, in a row. It's faster to check whether the rest of the row + the candidate digit equals 45, then it is to check for the candidate digit equals any of the other eight values for the row.

Why? Because you've already summed up the row digits previously. Just add the last candidate, and make one comparison.

There are faster ways to do this in a Sudoku program - several actually, some of which are blindingly fast ways - but the programming is not as intuitive.

Why? Because you've already summed up the row digits previously. Just add the last candidate, and make one comparison.
That is a really really really really really really really really really really really really really really really really really really really really big assumption -- you won't have needed the sum for anything, ever, before now; so why did you add it up?

12. This CAN be a relatively fast way to solve a puzzle, IF you design your program, to use it. It is NOT one of the fastest ways, imo.

The two fastest ways involve bit fiddling or using Knuth's "Dancing Links". Neither of these algorithms are suited for beginners, however. With some very carefully chosen optimizations, they are absolute SCREAMERS.

13. Thanks for the replies,and the lessons would really help a lot.

14. last question:
How can I add a whole row,column or box?(Should I do if(a[0]+a[1]+...+a[8] = 45)
printf(...

or is there a shorter way to do it?

And how can I make the program verify if a number is not repeated in a row,column,and a box?

15. Use some for() loops.