-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem29.cmd
More file actions
53 lines (44 loc) · 1.07 KB
/
Problem29.cmd
File metadata and controls
53 lines (44 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@ECHO OFF
SETLOCAL EnableDelayedExpansion
:: Project Euler Problem 29
:: How many distinct terms are in the sequence generated by
:: a^b for 2<=a<=100 and 2<=b<=100?
:: Instead of manually calculating each exponent, let's just count terms.
:: We'll do this by tracking which powers of which #s exist between 2-100,
:: then building a matrix of multiples of each exponent.
SET _Max=100
FOR /L %%A IN (2,1,%_Max%) DO (
IF NOT DEFINED _%%A CALL :FindExponents %%A
)
SET _Terms=0
FOR /L %%A IN (2,1,%_Max%) DO (
FOR /F "tokens=1,2 delims= " %%G IN ("!_%%A!") DO (
SET _Start=%%H*2
SET _Step=%%H
SET /A _End=_Max*_Step
FOR /L %%B IN (!_Start!,!_Step!,!_End!) DO (
IF NOT DEFINED _%%G_%%B (
IF %%B GTR 1 (
SET _%%G_%%B=1
SET /A _Terms+=1
)
)
)
)
)
ECHO:There are !_Terms! distinct terms.
ENDLOCAL
GOTO :EOF
:FindExponents
SET _%1=%1 1
SET _Mul=%1
SET _Prod=%1
SET _Exp=1
:ExpLoop
SET /A _Prod=_Prod*_Mul
SET /A _Exp+=1
IF !_Prod! LEQ 100 (
SET _!_Prod!=!_Mul! !_Exp!
GOTO ExpLoop
)
GOTO :EOF