previndexinfonext

code guessing, round #94 (completed)

started at ; stage 2 at ; ended at

specification

heyo! compute inverse square root. submissions may be written in any language.

the square root of a number x is a number y, such that y multiplied with itself is equal to x. the inverse square root is 1 divided by the square root.

your challenge, given a number, is to compute its inverse square root. as any language is allowed, there is no fixed API.

results

  1. 👑 kimapr +4 -1 = 3
    1. lychee (was oleander)
    2. olive
    3. seshoumara
    4. essaie
    5. Dolphy
    6. oleander (was lychee)
  2. Dolphy +4 -1 = 3
    1. olive (was oleander)
    2. oleander (was olive)
    3. seshoumara
    4. essaie
    5. lychee
    6. kimapr
  3. olive +2 -2 = 0
    1. kimapr (was oleander)
    2. seshoumara
    3. Dolphy (was essaie)
    4. oleander (was Dolphy)
    5. lychee
    6. essaie (was kimapr)
  4. oleander +1 -1 = 0
    1. lychee (was olive)
    2. seshoumara
    3. kimapr (was essaie)
    4. olive (was Dolphy)
    5. essaie (was lychee)
    6. Dolphy (was kimapr)
  5. essaie +2 -3 = -1
    1. lychee (was oleander)
    2. olive
    3. seshoumara
    4. kimapr (was Dolphy)
    5. Dolphy (was lychee)
    6. oleander (was kimapr)
  6. lychee +2 -3 = -1
    1. oleander
    2. Dolphy (was olive)
    3. olive (was seshoumara)
    4. essaie
    5. kimapr (was Dolphy)
    6. seshoumara (was kimapr)
  7. seshoumara +1 -5 = -4
    1. Dolphy (was oleander)
    2. essaie (was olive)
    3. oleander (was essaie)
    4. kimapr (was Dolphy)
    5. lychee
    6. olive (was kimapr)

entries

you can download all the entries

entry #1

written by oleander
submitted at
0 likes

guesses
comments 0

post a comment


isqrt.rs ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fn isqrt(num: f32) -> f32{
  let bnum: u32 = num.to_bits() >> 1;
  let mnum: u32 = 1597463007;
  let rnum: u32 = mnum.wrapping_sub(bnum);
  let mut fnum: f32 = f32::from_bits(rnum);
  for _ in 0..3 {
    fnum *= 1.5 - (0.5 *  num  * fnum.powf(2.0));
  }
  return fnum;
}

entry #2

written by olive
submitted at
0 likes

guesses
comments 0

post a comment


isr.lua ASCII text
1
return function(x) return x^-0.5 end

entry #3

written by seshoumara
submitted at
0 likes

guesses
comments 0

post a comment


isq.dc ASCII text
1
2
3
#!/usr/bin/dc -f

Fk1?v/p

entry #4

written by essaie
submitted at
0 likes

guesses
comments 0

post a comment


taswell.lua Unicode text, UTF-8 text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Entry = (
  (
    ¤ ^ 1
    do(dip(×100)
      ⊂⊙^
      ddd(-1)
    | ggg(>0)
    )
    dgg◌
  )!(byback(dipsubonmulfloobel %))
  floby√unjoi      # evil floating point bit level hacking
  (base 10) for(sub˙*|×2|backward ⊂@.un⋕) # what the fuck?
  bot
  ⍢(dddunjoin
    ⊃(mulonrow,>⊂ran10gi|backjoi⊙⋅⋅(˜⊂0)|gdi) # gFdi tbh
    (keebelo ≡,1(>0⬚0⊣ keebyne 0^fill0-)
     forlas (-1 length)
     ⊃(⬚0subtradgi|joimul2 gdgi |˜⊂+@0⋅⊙⋅⋅∘)
     ∩(by parbox byeq 0^
       drotry (negalengun□⊣)0)
    )!(⬚0 add ⊂0⊃ (floodiv|◿) 10)
  |> 0lenggggi
  )
  &pggdp
)
Entry 21 37    # doesn't work
Entry 0.5 1024 # zombocom

entry #5

written by Dolphy
submitted at
0 likes

guesses
comments 0

post a comment


not_so_fast_inv_sqrt.c ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <math.h>


double _sqrt(double x)
{
    double start = 0.0;
    double end = x;
    double mid;
    const double THRESHOLD = 1e-7;
    const int _ITER_LIMIT = 20;
    for(int i = 0; i < _ITER_LIMIT; i++) {
        mid = (start + end) / 2.0;
        if(fabs(mid * mid - x) < THRESHOLD) return mid;

        if(mid * mid > x) {
            end = mid;
        } else {
            start = mid;
        }
    }
    return mid;
}


double xa0c8a621(double x)
{
    return 1.0 / _sqrt(x);
}

entry #6

written by lychee
submitted at
0 likes

guesses
comments 0

post a comment


train.py Unicode text, UTF-8 text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import keras
import math
import numpy as np

# create a dataset
data = np.arange(0.001, 100, 0.00025)

np.random.shuffle(data)

# take a sample for testing purposes
split_data = np.array_split(data, 4)

x_train = np.concatenate((split_data[0], split_data[1], split_data[2]))
x_test = split_data[3]

split_data = None # wipe my temp variable

y_train = np.reciprocal(np.sqrt(x_train))
y_test = np.reciprocal(np.sqrt(x_test))

# set up a model / topology or whatever
model = keras.models.Sequential([
  keras.layers.Input(shape=(1,)),
  keras.layers.Dense(32, activation='relu'),
  keras.layers.Dense(16, activation='relu'),
  keras.layers.Dense(16, activation='relu'),
  keras.layers.Dense(1, activation='exponential')
])


# define a loss function
loss_fn = keras.losses.MeanSquaredLogarithmicError()

model.dropout = keras.layers.Dropout(0.0)

# compile the model, with the loss function
model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['mean_absolute_percentage_error'],
              auto_scale_loss=True)

# adjust the model to minimise loss
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=3)


# evaluate the model's performance
# for the purposes of this, I think 1 colour channel is fine
model.evaluate(x_test,  y_test, verbose=2)

while True: # bad bad bad error management, but good enough. Don't want it to hard crash after spending ages training.
  try:
    value = float(input("Enter test value: ").strip())
    prediction = model.predict(np.reshape(value, (1)))
    print("1 / sqrt({0}) ≈ {1}".format(value, prediction))
  except KeyboardInterrupt:
    break # (effectively just exit, but without importing sys)
  except:
    print("Invalid value! Please retry")

entry #7

written by kimapr
submitted at
0 likes

guesses
comments 0

post a comment


librsqrtss.fs ASCII text
1
2
3
4
5
( Code by olus2000 )


: rsqrtss ( F: r1 -- r2 )
  fsqrt 1e fswap f/ ;