previndexinfo

code guessing, round #94, stage 2 (guessing)

started at ; stage 2 since . guess by

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.

players

  1. Dolphy
  2. essaie
  3. kimapr
  4. lychee
  5. oleander
  6. olive
  7. seshoumara

entries

you can download all the entries

entry #1

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

comments 0

post a comment


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

entry #3

comments 0

post a comment


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

Fk1?v/p

entry #4

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

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

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

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/ ;