Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: OT: CSES Number Spiral algorithm Date: Thu, 20 Mar 2025 11:38:18 -0700 Organization: A noiseless patient Spider Lines: 32 Message-ID: <861puro8dh.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Thu, 20 Mar 2025 19:38:19 +0100 (CET) Injection-Info: dont-email.me; posting-host="7c01e97b5d1836204810413d59dbf156"; logging-data="3993682"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18MOp5QFso7KYwLz5b+EGCfsHpPUqDBx0Y=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:WjVr/DCkDNOnGazWttOnPHkSliE= sha1:wCnvJjQ4rAuKZ8kcFEThkxHODRU= Bytes: 2061 DFS writes: > I don't have a C question, but rather I'd like input about algorithmic > approaches. > > https://cses.fi/problemset/task/1071 > > It's an 'infinite grid'. You have to find the value at rows-columns > from 1 to 10^9. > > First thing you notice about the 5x5 grid is the values in > odd-numbered columns begin with the square of the column number, and > the values in even-numbered rows begin with the square of the row > number. > > I followed the number pattern and built a grid in Excel and expanded > it to 10x10 for more testing. > > https://imgur.com/x4VymmA > > Then coded 4 conditions solution > 1. row <= col and col odd (col * col) - row + 1 > 2. row <= col and col even ((col-1) * (col-1)) + row > 3. row > col and row odd ((row-1) * (row-1)) + col > 4. row > col and row even (row * row) - col + 1 > > My full C code submission was accepted the first time. > > How would you have done it? This posting is not appropriate for comp.lang.c. Putting "OT" in the subject line doesn't excuse its lack of suitability.