5 kyu

Fibo akin

495 of 1,996g964

Description:

Be u(n) a sequence beginning with:

u[1]  = 1,  u[2]  = 1,  u[3]  = 2,  u[4]  = 3,  u[5]  = 3,  u[6] = 4,

u[7]  = 5,  u[8]  = 5,  u[9]  = 6,  u[10] = 6,  u[11] = 6,  u[12] = 8,

u[13] = 8,  u[14] = 8,  u[15] = 10, u[16] = 9,  u[17] = 10, u[18] = 11,

u[19] = 11, u[20] = 12, u[21] = 12, u[22] = 12, u[23] = 12 etc...
  • How isu[8] calculated?

We have u[7] = 5 and u[6] = 4. These numbers tell us that we have to go backwards from index 8 to index 8 - 5 = 3 and to index 8 - 4 = 4 so to index 3 and 4.

u[3] = 2 and u[4] = 3 hence u[8] = u[3] + u[4] = 2 + 3 = 5.

  • Another example: let us calculate u[13]. At indexes 12 and 11 we have 8 and 6. Going backwards of 8 and 6 from 13 we get indexes 13 - 8 = 5 and 13 - 6 = 7.

u[5] = 3 and u[7] = 5 so u[13] = u[5] + u[7] = 3 + 5 = 8 .

Task

    1. Express u(n) as a function of n, u[n - 1], u[n - 2]. (not tested).
    1. Given two numbers n, k (integers > 2) write the function length_sup_u_k(n, k) or lengthSupUK or length-sup-u-k returning the number of terms u[i] >= k with 1 <= i <= n. If we look above we can see that between u[1] and u[23] we have four u[i] greater or equal to 12: length_sup_u_k(23, 12) => 4
    1. Given n (integer > 2) write the function comp(n) (cmp in COBOL) returning the number of times where a term of u is less than its predecessor up to and including u[n].

Examples:

u(900) => 455 (not tested)
u(90000) => 44337 (not tested)

length_sup_u_k(23, 12) => 4
length_sup_u_k(50, 10) => 35
length_sup_u_k(500, 100) => 304

comp(23) => 1 (since only u(16) < u(15))
comp(100) => 22
comp(200) => 63

Note: Shell

Shell tests only lengthSupUk

Algorithms
Recursion

More By Author:

Check out these other kata created by g964

Stats:

CreatedJun 28, 2016
PublishedJun 28, 2016
Warriors Trained11970
Total Skips5184
Total Code Submissions5404
Total Times Completed1996
Ruby Completions53
Python Completions495
Java Completions178
C# Completions115
Elixir Completions32
Clojure Completions23
JavaScript Completions253
CoffeeScript Completions9
TypeScript Completions64
Haskell Completions45
C++ Completions214
PHP Completions48
F# Completions21
C Completions190
Crystal Completions9
OCaml Completions24
Rust Completions100
Swift Completions49
Go Completions93
R Completions33
Shell Completions7
Kotlin Completions54
Fortran Completions12
Groovy Completions7
Julia Completions22
Scala Completions27
PowerShell Completions10
Nim Completions6
Reason Completions4
Lua Completions30
Pascal Completions8
Perl Completions7
COBOL Completions6
D Completions7
Erlang Completions5
Total Stars177
% of votes with a positive feedback rating88% of 337
Total "Very Satisfied" Votes267
Total "Somewhat Satisfied" Votes59
Total "Not Satisfied" Votes11
Total Rank Assessments8
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • g964 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • narayanswa30663 Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • akar-0 Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad