{ }codelper
stepping loopsP0371

Street lamps, with one at the very end

Street lamps are put up along a walking path that is L meters long. The first lamp stands at the start (the 0-meter point), and from there one lamp stands every s meters. The end of the path (the L-meter point) always gets a lamp, even if the spacing does not land on it. No point ever gets two lamps. One line gives L and s separated by a single space. Print how many meters each lamp is from the start, nearest first, one per line. For example, L = 10 and s = 3 give 0, 3, 6, 9, 10, and L = 10 and s = 5 give 0, 5, 10.

Input
1 ≤ L ≤ 1,000, 1 ≤ s ≤ 1,000 (all integers).
s may be larger than L
Output
One line per lamp, smallest first.
Digits only, no unit
Example 1 · stdin
10 3
stdout
0
3
6
9
10
Example 2 · stdin
10 5
stdout
0
5
10
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

A loop that jumps by a fixed step looks different in each language.
Python writes for x in range(0, L, s), and the end (L) is not included.
C, C++, Java, C#, JS and TS write for (x = 0; x < L; x += s), and Craykon writes for (x = 0; x < L; x += s) too.
Loop only up to just before L, then print L once, and the end point is neither doubled nor missing.
Trap one: looping while x <= L and then printing L again prints the end point twice whenever L is a multiple of s (example 2 becomes 0 5 10 10).
Trap two: looping to the end with range(0, L + 1, s) and never printing L separately loses the last lamp whenever L is not a multiple of s (example 1 becomes 0 3 6 9).
Trap three: counting iterations instead, printing i * s while i < L / s, gives different results by language.
In C, C++, Java and C#, / between integers keeps only the quotient, so 10 / 3 is 3, the loop runs for i = 0, 1, 2 and 9 is missing (example 1 becomes 0 3 6 10).
In JS, TS and Craykon / divides all the way to 3.333…, so the loop also runs for i = 3 and the answer is right.
Python stops with a TypeError because range(L / s) does not take a decimal, and after changing it to range(L // s) it loses 9 just like C.

Pythonmain.py · 5 lines
L, s = map(int, input().split())# range(0, L, s)는 0부터 s씩 건너뛰며, 끝(L)은 넣지 않는다for x in range(0, L, s):    print(x)print(L)   # 길 끝의 가로등은 따로 한 번
JavaScriptmain.js · 8 lines
const [L, s] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = [];// x += s로 s씩 건너뛴다. 끝(L)은 넣지 않는다for (let x = 0; x < L; x += s) {  out.push(x);}out.push(L); // 길 끝의 가로등은 따로 한 번console.log(out.join("\n"));
TypeScriptmain.ts · 9 lines
import { readFileSync } from "node:fs";const [L, s]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: number[] = [];// x += s로 s씩 건너뛴다. 끝(L)은 넣지 않는다for (let x = 0; x < L; x += s) {  out.push(x);}out.push(L); // 길 끝의 가로등은 따로 한 번console.log(out.join("\n"));
JavaMain.java · 13 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int L = sc.nextInt(), s = sc.nextInt();        // x += s로 s씩 건너뛴다. 끝(L)은 넣지 않는다        for (int x = 0; x < L; x += s) {            System.out.println(x);        }        System.out.println(L);   // 길 끝의 가로등은 따로 한 번    }}
C#Main.cs · 16 lines
using System; class Program{    static void Main()    {        string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int L = int.Parse(parts[0]), s = int.Parse(parts[1]);        // x += s로 s씩 건너뛴다. 끝(L)은 넣지 않는다        for (int x = 0; x < L; x += s)        {            Console.WriteLine(x);        }        Console.WriteLine(L);   // 길 끝의 가로등은 따로 한 번    }}
C++main.cpp · 11 lines
#include <iostream> int main() {    int L, s;    std::cin >> L >> s;    // x += s로 s씩 건너뛴다. 끝(L)은 넣지 않는다    for (int x = 0; x < L; x += s) {        std::cout << x << "\n";    }    std::cout << L << "\n";   // 길 끝의 가로등은 따로 한 번}
Cmain.c · 12 lines
#include <stdio.h> int main(void) {    int L, s;    if (scanf("%d %d", &L, &s) != 2) return 0;    /* x += s로 s씩 건너뛴다. 끝(L)은 넣지 않는다 */    for (int x = 0; x < L; x += s) {        printf("%d\n", x);    }    printf("%d\n", L);   /* 길 끝의 가로등은 따로 한 번 */    return 0;}
Craykonmain.cray · 9 lines
nums = Console.ReadNumbers();L = nums[0];s = nums[1];// x += s로 s씩 건너뛴다. 끝(L)은 넣지 않는다for (x = 0; x < L; x += s){    Console.WriteLine(x);}Console.WriteLine(L); // 길 끝의 가로등은 따로 한 번