How to resolve the algorithm Bulls and cows step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bulls and cows step by step in the PARI/GP programming language

Table of Contents

Problem Statement

Bulls and Cows   is an old game played with pencil and paper that was later implemented using computers.

Create a four digit random number from the digits   1   to   9,   without duplication. The program should:

The score is computed as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bulls and cows step by step in the PARI/GP programming language

Source code in the pari/gp programming language

bc()={
  my(u,v,bulls,cows);
  while(#vecsort(v=vector(4,i,random(9)+1),,8)<4,);
  while(bulls<4,
    u=input();
    if(type(u)!="t_VEC"|#u!=4,next);
    bulls=sum(i=1,4,u[i]==v[i]);
    cows=sum(i=1,4,sum(j=1,4,i!=j&v[i]==u[j]));
    print("You have "bulls" bulls and "cows" cows")
  )
};

  

You may also check:How to resolve the algorithm Forward difference step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Binary search step by step in the Perl programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Dart programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the Terraform programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Modula-3 programming language