Logo notas.itmens

Problem Set 2

The following problems can be solved with single-line dfns.

Eggs#

A recipe serving 4 people uses 3 eggs. Write the function Eggs which computes the number of eggs which need cracking to serve people. Using a fraction of an egg requires that a whole egg be cracked.

      Eggs 4
3
      Eggs 100
75
      Eggs ⍳12
1 2 3 3 4 5 6 6 7 8 9 9

Answer:

Eggs←{⌈3×⍵÷4}

To#

Write a function To which returns integers from to inclusive.

      3 To 3
3
      3 To 4
3 4
      1 To 7
1 2 3 4 5 6 7
      ¯3 To 5
¯3 ¯2 ¯1 0 1 2 3 4 5

BONUS: What if ⍺>⍵?

      3 To 5
3 4 5
      5 To 3
5 4 3
      5 To ¯2
5 4 3 2 1 0 ¯1 ¯2

Answer:

To←{¯1+⍺+⍳1+⍵-⍺}

For the bonus

To←{⍺+(×d)ׯ1+⍳1+|d←⍵-⍺}

Temperature#

The formula to convert temperature from Celsius (C) to Fahrenheit (F) in traditional mathematical notation is as follows:

\(T_F = 32+\frac{9}{5}T_C\)

Write the function CtoF to convert temperatures from Celcius to Farenheit.

      CtoF 11.3 23 0 16 ¯10 38
52.34 73.4 32 60.8 14 100.4

Answer:

CtoF←{32+9×⍵÷5}

Prime Time#

A prime number is a positive whole number greater than 1 which can be divided only by itself and 1 with no remainder.

Write a dfn which returns 1 if its argument is prime and 0 otherwise:

          IsPrime 21
0
          IsPrime 17
1

Answer:

IsPrime←{2<+/0=(⍳⍵)|⍵}