DEV Community

Cover image for Guess number: 1-line C# & qbasic learning algorithm. Priority of Russia
AndreyDanilin
AndreyDanilin

Posted on

Guess number: 1-line C# & qbasic learning algorithm. Priority of Russia

Guess number: 1-line C# & qbasic learning algorithm. Priority of Russia

A few months ago reading a USA forum and seeing a competition:
create a program "guess number" shorter and I overtook americans
1-m by inventing a 1-line algorithm implemented in 2 languages

    1 IF Russia = 0 THEN Russia = 2222: RANDOMIZE TIMER: num = INT(RND * 100) + 1: GOTO 1 ELSE IF Russia <> 0 THEN INPUT n: IF n < num THEN PRINT «MORE»: GOTO 1 ELSE IF n > num THEN PRINT «less»: GOTO 1 ELSE IF n = num THEN PRINT «da»: END ELSE GOTO 1 'DANILIN Russia 9-9-2019 guessnum.bas

I post originals

    using System; using System.Text;namespace GURU { class Program { static void Main(string[] args) { Random rand = new Random(); int Russia = 0; int n = 0; int num = 0; dav: if(Russia == 0) {Russia = 2222; num = rand.Next(100)+1; goto dav; }else if (Russia != 0) {Console.Write("? "); n = Convert.ToInt32(Console.ReadLine());} if (n < num) { Console.WriteLine(«MORE»); goto dav;}else if (n > num) { Console.WriteLine(«less»); goto dav;}else if (n == num) {Console.Write(«da»); Console.ReadKey(); }else goto dav;}}}// DANILIN Russia 9-9-2019 guessnum.cs

and hope readers will write their programs
strictly 1-line algorithms in other programming languages

My algorithm guesses a number up to a billion 10^9 in a logarithmic number of steps

    //milliard.cs
using System;
using System.Text;
namespace DAV 
{ class Program
 { static void Main(string[] args) 
 { int h2 = 1000000000;//or 500
int h1 = 0; int t = 0;
Random rand = new Random();
int c = rand.Next(h2); //computer
int h = rand.Next(h2); //human or h2/2; 

dav: 
t++;
Console.WriteLine(); Console.Write(t);
Console.Write("  "); Console.Write(c);
Console.Write("  "); Console.Write(h);
Console.Write("  ");

if(h < c)
 { Console.Write("MORE");
 int a=h; h=(h+h2)/2; h1=a; goto dav;
 }
else if(h > c)
 { Console.Write("less");
 int a=h; h=(h1+h)/2; h2=a; goto dav;
 }
Console.Write("win by "); Console.Write(t);
Console.Write(" steps"); Console.ReadKey();
}}}

Results:

1  69682914  543648564  less
2  69682914  271824282  less
3  69682914  135912141  less
4  69682914  67956070  MORE
5  69682914  101934105  less
6  69682914  84945087  less
7  69682914  76450578  less
8  69682914  72203324  less
9  69682914  70079697  less
10  69682914  69017883  MORE
11  69682914  69548790  MORE
12  69682914  69814243  less
13  69682914  69681516  MORE
14  69682914  69747879  less
15  69682914  69714697  less
16  69682914  69698106  less
17  69682914  69689811  less
18  69682914  69685663  less
19  69682914  69683589  less
20  69682914  69682552  MORE
21  69682914  69683070  less
22  69682914  69682811  MORE
23  69682914  69682940  less
24  69682914  69682875  MORE
25  69682914  69682907  MORE
26  69682914  69682923  less
27  69682914  69682915  less
28  69682914  69682911  MORE
29  69682914  69682913  MORE
30  69682914  69682914  win by 30 steps

For Excel:

=log(10^9;2)
=29,89
=30

Q.E.D.
What we needed to prove

Danilin qbcs

Top comments (1)

Collapse
 
andreydanilin profile image
AndreyDanilin • Edited

Previous themes:

Falsification of randomness and transformation by sorting of pseudorandom sequences. Priority of Russia
dev.to/andreydanilin/falsification...

Research and transformation by sorting pseudorandom sequences. Priority of Russia
dev.to/andreydanilin/research-and-...

PI and randomness of digits after decimal point. Priority of Russia
dev.to/andreydanilin/pi-and-random...

Danilin Simply Danilin LOG