{ }codelper
powersaccumulatingP0050

Powers that fit

One line gives integers a and N separated by a space.
Print every power of a — a^0, a^1, a^2, … — that is at most N, smallest first, one per line.
Since a^0 = 1 and N ≥ 1, the first line is always 1.

Input
2 ≤ a ≤ 1,000,000,000, 1 ≤ N ≤ 9,000,000,000,000,000 (9 × 10^15)
Output
each a^k ≤ N in order from k = 0, one per line
Example 1 · stdin
2 100
stdout
1
2
4
8
16
32
64
Example 2 · stdin
10 1000
stdout
1
10
100
1000
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

Instead of computing each power from scratch, keep multiplying the previous one by a (start at p = 1, then p *= a).
Three traps.
(1) ^ is not a power: in C, C++, Java, C#, JS and TS 2 ^ 3 is bitwise XOR, which is 1.
Only Python, JS and TS have a power operator, **; C/C++ have pow, Java Math.pow and C# Math.Pow (all double), and Craykon has no power at all, so repeat multiplication.
(2) Overflow: multiplying the last p near 9×10^15 by a (up to a billion) once more passes the long range (about 9.2×10^18).
C, C++, Java and C# wrap around without an error (in C/C++ it is undefined behaviour), so something like 65537^4 turns into a number below 9×10^15 and extra wrong lines appear.
Ask p > N / a before multiplying.
Python integers never overflow, and JS/TS/Craykon numbers are doubles that become inexact rather than overflowing — but by then they are already above N, so p <= N alone is correct.
(3) int: values pass 2^31, so C, C++, Java and C# need long (long long).

Pythonmain.py · 5 lines
a, n = map(int, input().split())p = 1  # a의 0제곱while p <= n:    print(p)    p *= a  # 파이썬 정수는 커져도 넘치지 않는다
JavaScriptmain.js · 5 lines
const [a, n] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = [];// p * a가 2^53을 넘으면 정확하지 않지만, 그때는 이미 n보다 커서 반복이 끝난다for (let p = 1; p <= n; p *= a) out.push(p);console.log(out.join("\n"));
TypeScriptmain.ts · 6 lines
import { readFileSync } from "node:fs";const [a, n]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: number[] = [];// p * a가 2^53을 넘으면 정확하지 않지만, 그때는 이미 n보다 커서 반복이 끝난다for (let p = 1; p <= n; p *= a) out.push(p);console.log(out.join("\n"));
JavaMain.java · 17 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        long a = sc.nextLong(), n = sc.nextLong();        StringBuilder out = new StringBuilder();        long p = 1;        while (true) {            out.append(p).append('\n');            // p * a는 long도 넘칠 수 있다 → 곱하기 전에 나눗셈으로 확인            if (p > n / a) break;            p *= a;        }        System.out.print(out);    }}
C#Main.cs · 18 lines
using System; class Program{    static void Main()    {        string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        long a = long.Parse(t[0]), n = long.Parse(t[1]);        long p = 1;        while (true)        {            Console.WriteLine(p);            // p * a는 long도 넘칠 수 있다(C#은 기본이 unchecked라 조용히 돈다) → 곱하기 전에 확인            if (p > n / a) break;            p *= a;        }    }}
C++main.cpp · 13 lines
#include <iostream> int main() {    long long a, n;    std::cin >> a >> n;    long long p = 1;    while (true) {        std::cout << p << '\n';        // p * a는 long long도 넘칠 수 있다 → 곱하기 전에 나눗셈으로 확인        if (p > n / a) break;        p *= a;    }}
Cmain.c · 14 lines
#include <stdio.h> int main(void) {    long long a, n;    scanf("%lld %lld", &a, &n);    long long p = 1;    for (;;) {        printf("%lld\n", p);        /* p * a는 long long도 넘칠 수 있다 → 곱하기 전에 나눗셈으로 확인 */        if (p > n / a) break;        p *= a;    }    return 0;}
Craykonmain.cray · 10 lines
수들 = Console.ReadNumbers();a = 수들[0];n = 수들[1];// 크레이콘 수는 실수라 넘치지 않는다. 2^53을 넘으면 정확하지 않지만 그때는 이미 n보다 크다p = 1;while (p <= n){    Console.WriteLine(p);    p *= a;}