[PYTHON] [CodeIQ] I tried to solve "This week's theme: Group made with number correspondence table" in Ruby (+11 languages)

I tried to solve "This week's theme: Group made with number correspondence table" that Mr. Masui engineer office had asked in CodeIQ with Ruby. ..

For the content and solution of the problem, see @ angel_p_57's Nice article. As a reference, if you know that the expected value of the number of groups when the number of participants is $ m $ is the $ m $ harmonic series (the partial sum of the $ m $ harmonic series), the rest will be specified. All I had to do was find $ m $ that exceeded my expectations.

Therefore, the code of the answer is as follows.

codeiq.2928.rb


i=s=0;n=gets.to_i;until s>n;i+=1;s+=1.0/i;end;p i

Also, it is [known] that the $ m $ harmonic number can be approximated by $ \ log m + \ gamma \ (\ gamma: Euler-Mascheroni \ constant) $ (http://mathworld.wolfram.com/Euler). -MascheroniConstant.html), the minimum $ m $ that this exceeds the expected value of $ n $ is

m= \lfloor\exp(n-\gamma)\rfloor+1

You can find the answer with $ O (1) $.

This time, the expected value test cases prepared were up to 6 at most, so I was able to write it with a slight shift as follows.

codeiq.2928.approximation.rb


p Math.exp(gets.oct-0.577).round

Answers in other languages

It works fine up to $ n = 8 $. I thought I'd try golfing in various languages, but Hard Code is better for writing ...

C

codeiq.2928.c


float s;main(i,n){scanf("%d",&n);while(s<=n)s+=1./i++;printf("%d",i-1);}

codeiq.2928.approximation.c


main(n){scanf("%d",&n);printf("%d",lround(exp(n-.577)));}

D

codeiq.2928.approximation.d


import std.stdio;import std.math;int n;void main(){readf("%d",&n);write(round(exp(n-.577)));}

Go

codeiq.2928.approximation.go


package main;import(."fmt";."math");func main(){n:=.0;Scan(&n);Print(int(Exp(n-.577)+.5))}

Haskell

codeiq.2928.approximation.hs


main=do
n<-readLn
putStr.show.round.exp$n-0.577

PHP

codeiq.2928.approximation.php


<?=round(exp(fgets(STDIN)-.577));

Perl

codeiq.2928.approximation.pl


print int exp(<>-.577)+.5

Python

codeiq.2928.approximation.py


print int(2.718**(input()-.576)+.5)

R

codeiq.2928.approximation.r


cat(round(exp(scan("stdin")-.577)))

Scala

codeiq.2928.approximation.scala


import scala.math._;object Main extends App{println(round(exp(readInt-.577)))}

Bash

codeiq.2928.approximation.sh


awk '{print int(exp($1-.577)+.5)}'</dev/stdin

JavaScript(spidermonkey)

codeiq.2928.approximation.spidermonkey.js


print(Math.round(Math.exp(readline()-.577)))

reference

Recommended Posts

[CodeIQ] I tried to solve "This week's theme: Group made with number correspondence table" in Ruby (+11 languages)
I tried to solve AOJ's number theory with Python
I made a prime number table output program in various languages
I tried to solve TSP with QAOA
I tried to discriminate a 6-digit number with a number discrimination application made with python
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
I tried to create a table only with Django
I tried to solve the soma cube with python
I tried to solve the problem with Python Vol.1
I made a command to generate a table comment in Django
I tried to solve a combination optimization problem with Qiskit
I tried to create an article in Wiki.js with SQLAlchemy
I tried to describe the traffic in real time with WebSocket
I tried to solve the ant book beginner's edition with python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
I tried to log in to twitter automatically with selenium (RPA, scraping)
I made a plugin to generate Markdown table from csv in Vim
How to write offline real time I tried to solve E11 with python
Environment maintenance made with Docker (I want to post-process GrADS in Python
I tried to solve AtCoder's depth-first search (DFS) in Python (result: TLE ...)
How to write offline real time I tried to solve E12 with python
I tried to predict the number of people infected with coronavirus in Japan by the method of the latest paper in China
I tried to predict the number of people infected with coronavirus in consideration of the effect of refraining from going out
I wanted to know the number of lines in multiple files, so I tried to get it with a command