There are 2 lists that store the digits of a number. Well, you need to find the GCD of these numbers. If you take the number 9239923923999 - 99992399 - everything seems to be okay, it works. If it's 99999999999999 and 9, then I'm just waiting .. How can I understand if I have an infinite loop somewhere, or is the code itself written crookedly and therefore it’s considered for a long time ?? If the 2nd option, how to speed ?? Algorithm NOD took this:
{ while (a != b) { if (a > b) { long tmp = a; a = b; b = tmp; } b = b - a; } return a; } Since it is easier to subtract two numbers stored in the list than modulo division. Actually, the subtraction itself:
{ Item * tempfirst = A->Head; Item * tempsecond = B->Head; while (tempfirst && tempsecond) { if (tempfirst->digit < tempsecond->digit) { Item * CurrentItem = tempfirst; CurrentItem->digit += 10; CurrentItem = CurrentItem->next; while (CurrentItem->digit <= 0) { CurrentItem->digit += 9; CurrentItem = CurrentItem->next; } CurrentItem->digit -= 1; } tempfirst->digit -= tempsecond->digit; tempfirst = tempfirst->next; tempsecond = tempsecond->next; } tempfirst = A->Tail; while (tempfirst) { if (tempfirst->digit != 0) break; A->Tail = tempfirst->prev; A->Tail->next = NULL; free(tempfirst); tempfirst = A->Tail; A->size--; } return A; } } For the rest, it seems to be sure .. But I feel in the subtraction somewhere that is a mistake, or something else.