Until One (Reduce Rules)
JavaScript
Medium
3 views
Problem Description
Given n, in one step you can do: if even n=n/2 else n=n-1. Count steps to reach 1. Print steps.
Input Format
One integer n.
Output Format
One integer steps.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);let n=BigInt(s);let steps=0n;while(n>1n){if(n%2n===0n)n/=2n;else n-=1n;steps++;}process.stdout.write(steps.toString());
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!